【发布时间】:2020-04-05 17:15:27
【问题描述】:
为什么我可以在一个上调用average() 方法,而在另一个上不行?它们不应该是等价的吗?
示例 1 - 有效
List<String> stringList = new ArrayList<>();
stringList.add("2");
stringList.add("4");
stringList.add("6");
// String array ("2","4", "6"
averageValue = stringList.stream()
.mapToInt(s -> Integer.valueOf(s))
.average()
.getAsDouble();
示例 2 - 未编译(已删除 mapToInt 调用,因为已传递整数流)
List<Integer> IntegerList = new ArrayList<>();
IntegerList.add(2);
IntegerList.add(4);
IntegerList.add(6);
averageValue = IntegerList.stream()
.average()
.getAsDouble();
问题,当我已经将整数流传递给 mapToInt 方法时,为什么还需要调用它?
【问题讨论】:
-
尼特:使用
Integer.parseInt而不是valueOf。
标签: java java-stream average