java listmap去重复_【求助】JAVA8 List

问题描述

如下所示 List 数据结构:

List> list = new ArrayList<>();

Map map1 = new HashMap<>();

map1.put("order_no", "123");

map1.put("quantity", 10);

map1.put("amount", 100);

Map map2 = new HashMap<>();

map2.put("order_no", "223");

map2.put("quantity", 15);

map2.put("amount", 150);

Map map3 = new HashMap<>();

map3.put("order_no", "123");

map3.put("quantity", 5);

map3.put("amount", 50);

Map map4 = new HashMap<>();

map4.put("order_no", "124");

map4.put("quantity", 6);

map4.put("amount", 60);

Map map5 = new HashMap<>();

map5.put("order_no", "223");

map5.put("quantity", 7);

map5.put("amount", 70);

list.add(map1);

list.add(map2);

list.add(map3);

list.add(map4);

list.add(map5);

有个需求是判断上述list中,是否存在有Map.key=order_no,其value重复,并将重复的项取出,如例子所示 最后应该会抓到order_no=123,223,这两笔订单,我目前的写法是:

//定义一个过渡用的list2 与list一致

List> list2 = new ArrayList<>();

list2.addAll(list);

List> collect = list.stream().filter(x->{

long count = list2.stream().filter(x2->x2.get("order_no").equals(x.get("order_no"))).count();

if(count>1) { //判断是否重复

return true;

}

return false;

}).collect(Collectors.groupingBy(x->x.get("order_no"))).entrySet().stream().map(x->{

Map tmp = new HashMap<>();

tmp.put("key_order", x.getKey());

tmp.put("order_list", x.getValue());

return tmp; //分组展示重复的订单数据

}).collect(Collectors.toList());

目前虽然功能是实现的,但是考虑到订单量有数万笔乃至更多,重新定义了一个一样的过渡用list这样的写法比较粗糙,效能也不高,想请教下大家有没有更简洁高效优雅些的方式 可以实现功能呢?

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值