【发布时间】:2018-02-25 23:33:18
【问题描述】:
我正在尝试接收具有变量vote(0 到 100)的对象流。我正在尝试将每个出现的次数计算到十位。例如:
23,44,48 returns 0:1, 1:2, 0:3, 2:4,...
我在这里做错了什么?
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
public class NewClass {
public static void main(String[] args){
class Temp{
Long vote=ThreadLocalRandom.current().nextLong(100);
}
ArrayList<Temp> t=new ArrayList();
t.add(new Temp());
t.add(new Temp());
t.add(new Temp());
Map<Integer, Long> counters = t.stream()
.collect(Collectors.groupingBy(p -> {
return ((int)p.vote/10);
}, Collectors.counting()));
Collection<Long> values = counters.values();
Integer[] res = values.toArray(new Long[values.size()]);
}
}
【问题讨论】:
-
错误是什么?
-
我相信 res 变量应该是 Long[] 类型。
-
@Aominè 是的,或者可能是
ArrayList的初始化(缺少<>),不记得是发出警告还是错误。 -
@FedericoPeraltaSchaffner 没有注意到这一点。大声喊叫!。
标签: java collections java-8