Java 8 frequency Object in array [duplicate]

与世无争的帅哥 提交于 2019-12-19 09:53:10

问题


I have an Object[] array

I need to create map Map<Obejct, Integer>, where Integer value contains frequency of key Object in array.

How can i do it in java 8 style, using Collectors?


回答1:


You can do (I hope I don't have any typos) :

Map<Object,Long> map = Stream.of(array)
                                .collect(Collectors.groupingBy(o -> o,
                                                               Collectors.counting()));

This should group the elements of the array by equality and count the number of Objects in each group.



来源:https://stackoverflow.com/questions/29450169/java-8-frequency-object-in-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!