Java 8中有许多有用的新功能。例如,我可以使用流迭代遍历对象列表,然后对Object
实例的特定字段的值进行求和。例如:
public class AClass {
private int value;
public int getValue() { return value; }
}
Integer sum = list.stream().mapToInt(AClass::getValue).sum();
因此,我想问是否有办法构建一个String
,将多个实例的toString()
方法的输出连接成一行。
List<Integer> list = ...
String concatenated = list.stream().... //concatenate here with toString() method from java.lang.Integer class
假设list
包含整数1
、2
和3
,我希望concatenated
是"123"
或"1,2,3"
。