java找出最大的对象,从Java 8流中获取具有最大频率的对象

I have an object with city and zip fields, let's call it Record.

public class Record() {

private String zip;

private String city;

//getters and setters

}

Now, I have a collection of these objects, and I group them by zip using the following code:

final Collection records; //populated collection of records

final Map> recordsByZip = records.stream()

.collect(Collectors.groupingBy(Record::getZip));

So, now I have a map where the key is the zip and the value is a list of Record objects with that zip.

What I want to get now is the most common city for each zip.

recordsByZip.forEach((zip, records) -> {

final String mostCommonCity = //get most common city for these records

});

I would like to do this with all stream operations. For example, I am able to get a map of the frequency for each city by doing this:

recordsByZip.forEach((zip, entries) -> {

final Map frequencyMap = entries.stream()

.map(GisSectorFileRecord::getCity)

.filter(StringUtils::isNotBlank)

.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

});

But I would like to be able to do a single-line stream operation that will just return the most frequent city.

Are there any Java 8 stream gurus out there that can work some magic on this?

Here is an ideone sandbox if you'd like to play around with it.

解决方案

You could have the following:

final Map mostFrequentCities =

records.stream()

.collect(Collectors.groupingBy(

Record::getZip,

Collectors.collectingAndThen(

Collectors.groupingBy(Record::getCity, Collectors.counting()),

map -> map.entrySet().stream().max(Map.Entry.comparingByValue()).get().getKey()

)

));

This groups each records by their zip, and by their cities, counting the number of cities for each zip. Then, the map of the number of cities by zip is post-processed to keep only the city having the maximum count.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值