Using Java 8 to convert a list of objects into a string obtained from the toString() method

前端 未结 13 1722
太阳男子
太阳男子 2020-11-28 02:02

There are a lot of useful new things in Java 8. E.g., I can iterate with a stream over a list of objects and then sum the values from a specific field of the Object

相关标签:
13条回答
  • 2020-11-28 02:21

    There is a method in the String API for those "joining list of string" usecases, you don't even need Stream.

    List<String> myStringIterable = Arrays.asList("baguette", "bonjour");
    
    String myReducedString = String.join(",", myStringIterable);
    
    // And here you obtain "baguette,bonjour" in your myReducedString variable
    
    0 讨论(0)
  • 2020-11-28 02:21

    Can we try this.

    public static void main(String []args){
            List<String> stringList = new ArrayList<>();
            for(int i=0;i< 10;i++){
                stringList.add(""+i);
            }
            String stringConcated = String.join(",", stringList);
            System.out.println(stringConcated);
    
        }
    
    0 讨论(0)
  • 2020-11-28 02:23

    One simple way is to append your list items in a StringBuilder

       List<Integer> list = new ArrayList<>();
       list.add(1);
       list.add(2);
       list.add(3);
    
       StringBuilder b = new StringBuilder();
       list.forEach(b::append);
    
       System.out.println(b);
    

    you can also try:

    String s = list.stream().map(e -> e.toString()).reduce("", String::concat);
    

    Explanation: map converts Integer stream to String stream, then its reduced as concatenation of all the elements.

    Note: This is normal reduction which performs in O(n2)

    for better performance use a StringBuilder or mutable reduction similar to F. Böller's answer.

    String s = list.stream().map(Object::toString).collect(Collectors.joining(","));
    

    Ref: Stream Reduction

    0 讨论(0)
  • 2020-11-28 02:23
    List<String> list = Arrays.asList("One", "Two", "Three");
        list.stream()
                .reduce("", org.apache.commons.lang3.StringUtils::join);
    

    Or

    List<String> list = Arrays.asList("One", "Two", "Three");
            list.stream()
                    .reduce("", (s1,s2)->s1+s2);
    

    This approach allows you also build a string result from a list of objects Example

    List<Wrapper> list = Arrays.asList(w1, w2, w2);
            list.stream()
                    .map(w->w.getStringValue)
                    .reduce("", org.apache.commons.lang3.StringUtils::join);
    

    Here the reduce function allows you to have some initial value to which you want to append new string Example:

     List<String> errors = Arrays.asList("er1", "er2", "er3");
                list.stream()
                        .reduce("Found next errors:", (s1,s2)->s1+s2);
    
    0 讨论(0)
  • 2020-11-28 02:25
    String actual = list.stream().reduce((t, u) -> t + "," + u).get();
    
    0 讨论(0)
  • 2020-11-28 02:27

    The other answers are fine. However, you can also pass Collectors.toList() as parameter to Stream.collect() to return the elements as an ArrayList.

    System.out.println( list.stream().map( e -> e.toString() ).collect( toList() ) );
    
    0 讨论(0)
提交回复
热议问题