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

前端 未结 13 1842
太阳男子
太阳男子 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 myStringIterable = Arrays.asList("baguette", "bonjour");
    
    String myReducedString = String.join(",", myStringIterable);
    
    // And here you obtain "baguette,bonjour" in your myReducedString variable
    

提交回复
热议问题