Using java 8 join string values example with different-different ways are specified here like using Collectors.joining while using stream, collect function of stream, StringJoiner class.

List of String Joining

Example 1:  Separator using Collectors.joining()

Implementations of Collector that implement various useful reduction operations, such as accumulating elements into collections, summarizing elements according to various criteria, etc.

        java.util.List<String> strings = Arrays.asList("Zohne", "Redy", "Zohne", "Redy", "Stome");
        System.out.println("Join values by ,");
        String joinStrings = strings.stream().collect(Collectors.joining(","));
        System.out.println(joinStrings);

Output:

Join values by ,
Zohne,Redy,Zohne,Redy,Stome

Example 2: List of String Joining with Prefix and Sufix using Collectors.joining()

        java.util.List<String> strings = Arrays.asList("Zohne", "Redy", "Zohne", "Redy", "Stome");
        System.out.println("Join values by ,");
        String joinStrings = strings.stream().collect(Collectors.joining(",", "Those are names ", " of selected people.."));
        System.out.println(joinStrings);

 Output:

Those are names Zohne,Redy,Zohne,Redy,Stome of selected people..

Example 3: Unique String Joining using Collectors.joining()

java.util.List<String> strings = Arrays.asList("Zohne", "Redy", "Zohne", "Redy", "Stome");
        String joinStrings = strings.stream().distinct().collect(Collectors.joining(","));
        System.out.println(joinStrings);

 Output:

Zohne,Redy,Stome

Example 4: List of String Joining using Stream collect() function

java.util.List<String> strings = Arrays.asList("Zohne", "Redy", "Zohne", "Redy", "Stome");
        StringBuffer joinString= strings.stream().collect(StringBuffer::new, (StringBuffer stringBuffer, String s) -> {
            stringBuffer.append(s).append(",");
        }, StringBuffer::append);

        System.out.printf(joinString.toString());

 Output:

Zohne,Redy,Zohne,Redy,Stome,

String Joing

Example 5: String Joining using String.join

System.out.print(String.join("-","15","Jan","2017"));

Output:

15-Jan-2017

Example 6: String joining using StringJoiner

StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.

Prior to adding something to the StringJoiner, its sj.toString() method will, by default, return prefix + suffix. However, if the setEmptyValue method is called, the emptyValue supplied will be returned instead. This can be used, for example, when creating a string using set notation to indicate an empty set, i.e. "{}", where the prefix is "{", the suffix is "}" and nothing has been added to the StringJoiner.

public static void example5(){
        StringJoiner sj = new StringJoiner(",", "[", "]");
        sj.add("George").add("Sally").add("Fred");
        String desiredString = sj.toString();
        System.out.printf(desiredString);
    }

Output:

[George,Sally,Fred]

 

Refrences:

https://stackoverflow.com/questions/1751844/java-convert-liststring-to-a-string

Java 8 StringJoiner Document

Java 8 Stream Collectors

 

 

 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No
Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *