Add delimiter with in string using java 8 stream api
The provided Java code demonstrates a common and efficient way to join elements of a
List into a single String, while adding a delimiter, prefix, and suffix using the Stream API introduced in Java 8.Here's a breakdown of the code:
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class AddDlimetersWithInStrings {
public static void main(String[] str){
List<String> input= Arrays.asList("Rajesh","Kumar","R4R");
String outputStr=input.stream().collect(Collectors.joining(",","[","]"));
System.out.println(outputStr);
}
}
Explanation
import java.util.Arrays;- This line imports the
Arraysutility class, which provides static methods for manipulating arrays, including converting them to lists.
- This line imports the
import java.util.List;- This imports the
Listinterface, a key part of the Java Collections Framework that represents an ordered collection of elements, allowing duplicates and access by index.
- This imports the
import java.util.stream.Collectors;- This imports the
Collectorsclass, which offers various static methods to implement reduction operations on Streams, such as collecting elements into different collections or joining them into a string.
- This imports the
public class AddDlimetersWithInStrings { ... }- This defines a public class named
AddDlimetersWithInStringsthat contains themainmethod, the entry point for the Java application.
- This defines a public class named
public static void main(String[] str){ ... }- This is the
mainmethod where the program execution begins.
- This is the
List<String> input = Arrays.asList("Rajesh", "Kumar", "R4R");- An immutable
ListofStringobjects calledinputis created using theArrays.asList()method. This list is initialized with the strings "Rajesh", "Kumar", and "R4R".
- An immutable
String outputStr = input.stream().collect(Collectors.joining(",", "[", "]"));- This is the core of the example, demonstrating the use of Java 8 Streams and the
Collectors.joining()method. input.stream(): Converts theinputlist into a stream ofStringelements. Streams provide a functional way to process sequences of data..collect(...): This is a terminal operation that initiates the processing of the stream elements and accumulates them into a final result.Collectors.joining(",", "[", "]"): This is a static method of theCollectorsclass that returns aCollector. This particularjoiningcollector has three arguments:,: The delimiter to be inserted between the elements in the final string.[: The prefix to be added at the beginning of the final string.]: The suffix to be added at the end of the final string.
- The
Collectors.joining()method efficiently concatenates the elements of the stream into a singleString, inserting the specified delimiter between them and wrapping the entire result with the given prefix and suffix.
- This is the core of the example, demonstrating the use of Java 8 Streams and the
System.out.println(outputStr);- This line prints the final
outputStrto the console.
- This line prints the final
Output
[Rajesh,Kumar,R4R]