Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个Object[] array
Object[] array
我需要创建 map Map<Obejct, Integer>,其中Integervalue 包含数组中键 Object 的频率。
Map<Obejct, Integer>
Integer
我如何以 java 8 风格做到这一点,使用Collectors?
Collectors
你可以这样做(我希望我没有任何错别字):
Map<Object,Long> map = Stream.of(array) .collect(Collectors.groupingBy(o -> o, Collectors.counting()));
这应该按相等对数组的元素进行分组,并计算每组中对象的数量。