StringJoiner

package java8examples;

import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;

public class StringJoinerExp {

public static void main (String args[]){
StringJoiner sj = new StringJoiner("/", "prefix-", "-suffix");
        sj.add("2016");
        sj.add("02");
        sj.add("26");
        String result = sj.toString(); //prefix-2016/02/26-suffix
        System.out.println(" result : "+result);
       
       
       // StringJoiner is used internally by static String.join().
        String result1 = String.join("-", "2015", "10", "31" );
        System.out.println("StringJoiner  result1 : "+result1);
       
        // Collectors.joining
        // Join List<String> example.
        List<String> list = Arrays.asList("java", "python", "nodejs", "ruby");
      //java | python | nodejs | ruby
    String result2 = list.stream().map(x -> x).collect(Collectors.joining(" | "));
   
    System.out.println("Collectors.joining  result2: "+result2);
}

}

Comments

Popular posts from this blog

Codity Test and results

TTD - Test Driven Development for Java Programmers