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
Arrays
utility class, which provides static methods for manipulating arrays, including converting them to lists.
- This line imports the
import java.util.List;
- This imports the
List
interface, 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
Collectors
class, 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
AddDlimetersWithInStrings
that contains themain
method, the entry point for the Java application.
- This defines a public class named
public static void main(String[] str){ ... }
- This is the
main
method where the program execution begins.
- This is the
List<String> input = Arrays.asList("Rajesh", "Kumar", "R4R");
- An immutable
List
ofString
objects calledinput
is 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 theinput
list into a stream ofString
elements. 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 theCollectors
class that returns aCollector
. This particularjoining
collector 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
outputStr
to the console.
- This line prints the final
Output
[Rajesh,Kumar,R4R]