我知道我的问题与使用Java8的Count int事件非常类似 ,但我仍然无法解决我的情况,这必须更容易解决.
需要计算整数流中整数重复的次数(将来自文件,最多可达1000000个整数).我认为创建一个地图可能很有用,其中Integer将是一个Key,并且出现次数将是一个值.
例外是
错误:(61,66)java:接口java.util.stream.IntStream中的方法收集不能应用于给定的类型;
required:java.util.function.Supplier,java.util.function.ObjIntConsumer,java.util.function.BiConsumer found:java.util.stream.Collector> reason:无法推断类型变量R(实际和正式)参数列表长度不同)
但是,在Java 8中有一个Collectors.groupingBy,应该就足够了
Collector<T, ?, Map<K, D>> groupingBy(Function<? super T, ? extends K> classifier,Collector<? super T, A, D> downstream)
Run Code Online (Sandbox Code Playgroud)
问题是我的代码没有编译,我没有看到 - 为什么.我把它简化为:
Map<Integer,Integer> result = IntStream.range(0,100).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
Run Code Online (Sandbox Code Playgroud)
不编译的原因是什么?先感谢您 :)
Pet*_*rey 15
IntStream有一种方法collect,其中第二个参数对int非对象进行操作.使用boxed()变成IntStream一个Stream<Integer>
还counting()返回一个long.
Map<Integer, Long> result = IntStream.range(0, 100).boxed()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1886 次 |
| 最近记录: |