R4R Style Learning Portal

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
  1. import java.util.Arrays;
    • This line imports the Arrays utility class, which provides static methods for manipulating arrays, including converting them to lists.
  2. 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.
  3. 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.
  4. public class AddDlimetersWithInStrings { ... }
    • This defines a public class named AddDlimetersWithInStrings that contains the main method, the entry point for the Java application.
  5. public static void main(String[] str){ ... }
    • This is the main method where the program execution begins.
  6. List<String> input = Arrays.asList("Rajesh", "Kumar", "R4R");
    • An immutable List of String objects called input is created using the Arrays.asList() method. This list is initialized with the strings "Rajesh", "Kumar", and "R4R".
  7. 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 the input list into a stream of String 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 the Collectors class that returns a Collector. This particular joining 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 single String, inserting the specified delimiter between them and wrapping the entire result with the given prefix and suffix.
  8. System.out.println(outputStr);
    • This line prints the final outputStr to the console.
 
Output
[Rajesh,Kumar,R4R]