Java 8中有很多有用的新功能。例如,我可以在对象列表上迭代流,然后从对象实例的特定字段中求和。如。
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
假设列表包含整数1,2,3,我期望连接的是“123”或“1,2,3”。