0% found this document useful (0 votes)
3 views2 pages

Immutable vs Mutable Lists in Java

The document explains the String.join method in Java, including its syntax and usage with examples. It also discusses how to implement a custom join method using StringBuilder and highlights key differences between mutable and immutable lists in Java. Additionally, it covers the use of Arrays.stream and Collectors.joining for joining strings in Java 8.

Uploaded by

SAITEJA GUNDAPU
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Immutable vs Mutable Lists in Java

The document explains the String.join method in Java, including its syntax and usage with examples. It also discusses how to implement a custom join method using StringBuilder and highlights key differences between mutable and immutable lists in Java. Additionally, it covers the use of Arrays.stream and Collectors.joining for joining strings in Java 8.

Uploaded by

SAITEJA GUNDAPU
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

String Join Method :

Syntax:
=======
[Link]("delimiter","character Sequence 1","Character Sequence 2");

String stringV1 = [Link](" ", TEXT_1, TEXT_2, TEXT_3, TEXT_4, TEXT_5);


String stringNullCase=[Link]("-","a",null,"b",null);
[Link](stringNullCase);
Output:a-null-b-null

public static String joinByDelimiterV1(char delimiter, String... args) {


//Create a String Builder
StringBuilder result = new StringBuilder();
int i = 0;
for (i = 0; i < [Link] - 1; i++) {
[Link](args[i]).append(delimiter);
}
[Link](args[i]);
return [Link]();
}

Logic:
Create a StringBuilder and then append it till the last but one element and append
the delimoiter
[Link](args[i]).append(delimiter);
and then at last add the
.append(args[i]) and then convert the String
=========================================================

In Java 8 how to write ?


Usng
[Link](CharacterSequence);

[Link](Can take one parametre i.e array of anytype)


[Link](array of any type, int start Index, int last Index,)

For Ex:

[Link](argsArray whch is String array, 0, length of the String Array);

[Link]()

//======================= Key Important


Points========================================
//=================================================================================
===

[Link] returns a mutable list


while the list returned by [Link] is immutable;

List<Integer> list = [Link](1, 2, null);


[Link](1, 10); // OK
List<Integer> list = [Link](1, 2, 3);
[Link](1, 10); // Fails with UnsupportedOperationException

Integer[] array = {1,2,3};


List<Integer> list = [Link](array);
array[1] = 10;
[Link](list); // Prints [1, 10, 3]

Integer[] array = {1,2,3};


List<Integer> list = [Link](array);
array[1] = 10;
[Link](list); // Prints [1, 2, 3]

You might also like