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

STR Decl Join Immutable List

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)
19 views2 pages

STR Decl Join Immutable List

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
You are on page 1/ 2

String Join Method :

Syntax:
=======
String.join("delimiter","character Sequence 1","Character Sequence 2");

String stringV1 = String.join(" ", TEXT_1, TEXT_2, TEXT_3, TEXT_4, TEXT_5);


String stringNullCase=String.join("-","a",null,"b",null);
System.out.println(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 < args.length - 1; i++) {
result.append(args[i]).append(delimiter);
}
result.append(args[i]);
return result.toString();
}

Logic:
Create a StringBuilder and then append it till the last but one element and append
the delimoiter
result.append(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
Collectors.joining(CharacterSequence);

Arrays.Stream(Can take one parametre i.e array of anytype)


Arrays.Stream(array of any type, int start Index, int last Index,)

For Ex:

Arrays.Stream(argsArray whch is String array, 0, length of the String Array);

Arrays.Stream()

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


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

Arrays.asList returns a mutable list


while the list returned by List.of is immutable;

List<Integer> list = Arrays.asList(1, 2, null);


list.set(1, 10); // OK
List<Integer> list = List.of(1, 2, 3);
list.set(1, 10); // Fails with UnsupportedOperationException

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


List<Integer> list = Arrays.asList(array);
array[1] = 10;
System.out.println(list); // Prints [1, 10, 3]

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


List<Integer> list = List.of(array);
array[1] = 10;
System.out.println(list); // Prints [1, 2, 3]

You might also like