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

前端 未结 13 1796
太阳男子
太阳男子 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:47

    There is a collector joining in the API. It's a static method in Collectors.

    list.stream().map(Object::toString).collect(Collectors.joining(","))
    

    Not perfect because of the necessary call of toString, but works. Different delimiters are possible.

提交回复
热议问题