This question already has an answer here:
- Word frequency count Java 8 8 answers
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
?
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