Java 8中有许多有用的新东西.例如,我可以在一个对象列表上迭代一个流,然后对来自Object实例的特定字段的值求和.例如
public class AClass {
private int value;
public int getValue() { return value; }
}
Integer sum = list.stream().mapToInt(AClass::getValue).sum();
Run Code Online (Sandbox Code Playgroud)
因此,我问是否有任何方法可以构建一个String连接toString()方法的输出与单行中的实例.
List<Integer> list = ...
String concatenated = list.stream().... //concatenate here with toString() method from java.lang.Integer class
Run Code Online (Sandbox Code Playgroud)
假设list包含整数1,2并且3我希望它concatenated是"123"或"1,2,3".