java8 stream流,list元素string转int,求和

  • IntStream mapToInt(ToIntFunction mapper);

  • LongStream mapToLong(ToLongFunction mapper);

  • DoubleStream mapToDouble(ToDoubleFunction mapper)

以上方法截自java8源码:package java.util.stream;

public class Demo {
    public static void main(String[] args) {
        List stringList = new ArrayList<>();
        stringList.add("1");
        stringList.add("5");
        stringList.add("3");
        stringList.add("2");
        int sum = stringList.stream().mapToInt(Integer::parseInt).sum();
        System.out.println("sum = " + sum); // 控制台输出 sum = 11
    }
}

你可能感兴趣的:(笔记,java)