Java 8频率对象在数组中 [英] Java 8 frequency Object in array

查看:174
本文介绍了Java 8频率对象在数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Object []数组



我需要创建地图 Map< ; Obejct,Integer> ,其中整数值包含数组中关键对象的频率。



我怎么能用java 8风格,使用收集器

解决方案

你可以这样做(我希望我没有任何拼写错误):

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

这应该按等号对数组的元素进行分组,并计算每个组中的对象数。 / p>

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.

这篇关于Java 8频率对象在数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆