list实体与map之间处理

2 篇文章 0 订阅
2 篇文章 0 订阅

list实体对象值进行叠加处理

即:针对实体里某个属性出现的次数进行+1

具体实体:
List<HealthyResponse> li = new ArrayList<HealthyResponse>();
 HealthyResponse d = new HealthyResponse();
 d.setText("绿码");
 d.setValue(Long.valueOf(0));
 
 HealthyResponse dd = new HealthyResponse();
 dd.setText("绿码");
 dd.setValue(Long.valueOf(0));

 HealthyResponse ddd = new HealthyResponse();
 ddd.setText("红码");
 ddd.setValue(Long.valueOf(0));

 HealthyResponse dddd = new HealthyResponse();
 dddd.setText("黄码");
 dddd.setValue(Long.valueOf(0));

 HealthyResponse ddddd = new HealthyResponse();
 ddddd.setText("黄码");
 ddddd.setValue(Long.valueOf(0));

 HealthyResponse dddddd = new HealthyResponse();
 dddddd.setText("黄码");
 dddddd.setValue(Long.valueOf(0));
 

 li.add(d);
 li.add(dd);
 li.add(ddd);
 li.add(dddd);
 li.add(ddddd);
 li.add(dddddd);

方法一:

Map<String, Integer> number = new HashMap<>();
if (li != null) {
    for (HealthyResponse idCard : li) {
        number.merge(idCard.getText(), 1, Integer::sum);
    }
}
System.out.println(number);

结果:

   {红码=1, 绿码=2, 黄码=3}

方法二:

 Map<String, Long> m 
 = li.stream().collect(Collectors.groupingBy(HealthyResponse::getText, Collectors.counting()));
 System.out.println(m);

结果:

  {"红码":1,"绿码":2,"黄码":3}

针对list实体里的某一个属性的某一个点进行叠加处理

例:根据实体中的时间里的小时进行统计叠加

 VisitList visit = new VisitList();
 visit.setCreateTime("2021-06-28 15:31:59");

 VisitList visit1 = new VisitList();
 visit1.setCreateTime("2021-06-30 00:00:00");

 VisitList visit2 = new VisitList();
 visit2.setCreateTime("2021-06-28 15:33:42");

 List<VisitList> visitLists = new ArrayList<>();
 visitLists.add(visit);
 visitLists.add(visit1);
 visitLists.add(visit2);





 Map<Object, Long> groupByMonth = visitLists.stream().collect(
                Collectors.groupingBy(p -> LocalDateTime.parse(p.getCreateTime(), dtf).getHour(), Collectors.counting()));

 System.out.println(FastJsonConvertUtils.convertObjectToJSON(groupByMonth));

结果:

         {0:1,15:2}

map转list实体

 List<HealthyResponse> test  = 
m.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue))
                .map(
                e -> new HealthyResponse(e.getKey(), e.getValue())).collect(Collectors.toList()
                );

结果:

[{"text":"红码","value":1},{"text":"绿码","value":2},{"text":"黄码","value":3}]

list实体中选取其中几条属性形成新的list

 List<Park> list = getParkIds();
 List<ParkDto> parkDtoList = list.stream().
             filter(s ->s.getId() != null && s.getName() != null).
             map(e -> new ParkDto(e.getId() ,e.getName())).
             collect(Collectors.toList());

结果:

"新的": [
            {
                "id": 472,
                "name": "绍兴电力设备有限公司"
            },
            {
                "id": 471,
                "name": "绍兴诚邦高新纤维科技有限公司"
            },
            {
                "id": 427,
                "name": "浙江伊夫服饰有限公司"
            },
            {
                "id": 428,
                "name": "绍兴钱江亚润家居用品有限公司"
            }
       ]  

list实体单属性形成新的list

List<Park> list = getParkIds();
List<Long> parkIds = getParkIds().stream().
           filter( s -> s.getId() != null ).
           map(Park::getId).collect(Collectors.toList());

结果:

"parkIds": [
            472,
            471,
            427,
            428,
            413,
            402,
            444,
            430,
            398,
            418,
            412,
            407,
            401,
            426
          ]

list实体合并,属性相同的进行叠加

        List<HealthyResponse> list1 = new ArrayList<>();

        HealthyResponse h = new HealthyResponse();
        h.setText("黑码");
        h.setValue(Long.valueOf(0));
       
        HealthyResponse hh = new HealthyResponse();
        hh.setText("红码");
        hh.setValue(Long.valueOf(7));
        list1.add(h);
        list1.add(hh);

        //list合并
        test.addAll(list1);
      

        System.out.println("合并数据"+FastJsonConvertUtils.convertObjectToJSON(test));
        List<HealthyResponse> ne = new ArrayList<>();

        test.parallelStream().collect(Collectors.groupingBy(o -> (o.getText()), Collectors.toList())).forEach(
                (id, transfer) -> {
                    transfer.stream().reduce((b, c) -> new HealthyResponse(b.getText(), b.getValue() + c.getValue())).ifPresent(ne::add);
                }
        );

    
        System.out.println("截取前5条数据"+FastJsonConvertUtils.convertObjectToJSON(li.subList(0, 5)));

        System.out.println("list叠加之后数据" + FastJsonConvertUtils.convertObjectToJSON(ne));

结果:

合并数据:[{"text":"红码","value":1},{"text":"绿码","value":2},{"text":"黄码","value":3},{"text":"黑码","value":0},{"text":"红码","value":7}]

截取前5条数据:[{"text":"绿码","value":0},{"text":"绿码","value":0},{"text":"红码","value":0},{"text":"黄码","value":0},{"text":"黄码","value":0}]

list叠加之后数据:[{"text":"红码","value":8},{"text":"黑码","value":0},{"text":"绿码","value":2},{"text":"黄码","value":3}]

list实体两条属性分别相加,并去重

例:现有list为:实现结果应该为当id为97时,up与对应一致的up相加,down与对应一致的down相加,形成一个结果。

   [
        {
            "id": 97,
            "name": "测试栏目",
            "up": 1,
            "down": 0
        },
        {
            "id": 97,
            "name": "测试栏目",
            "up": 1,
            "down": 7
        },
        {
            "id": 98,
            "name": "测试首页",
            "up": 1,
            "down": 1
        }
    ]

方法:

//初始化res数据,针对该InforCountResponse实体写构造方法(每个属性都要)
List<InforCountResponse> res = new ArrayList<>();   


List<InforCountResponse> ne = new ArrayList<>();

res.parallelStream().collect(Collectors.groupingBy(o -> (o.getId()), Collectors.toList())).forEach(
        (id, transfer) -> {
            transfer.stream().reduce((b, c) -> new InforCountResponse(b.getId(),b.getName(),b.getUp() + c.getUp() ,b.getDown()+c.getDown())).ifPresent(ne::add);
        }
);

结果:

   [
        {
            "id": 97,
            "name": "测试栏目",
            "up": 2,
            "down": 8
        },
        {
            "id": 98,
            "name": "测试首页",
            "up": 1,
            "down": 1
        }
    ] 

map合并,同key则value叠加

例:

map1={0:0,1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0,13:0,14:0,15:0,16:0,17:0,18:0,19:0,20:0,21:0,22:0,23:0}

map2= {0:1,15:2}

实现目标为:
{0:1,1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0,13:0,14:0,15:2,16:0,17:0,18:0,19:0,20:0,21:0,22:0,23:0}

  Map<Object, Long> groupByMonth = visitLists.stream().collect(
                Collectors.groupingBy(p -> LocalDateTime.parse(p.getCreateTime(), dtf).getHour(), Collectors.counting()));

  Map<Object, Integer> jj = new HashMap<>();
  for (int i = 0; i <= 23; i++) {
         jj.put(i, 0);
   }
   
 Map<Object, Integer> inital = jj;
        groupByMonth.forEach(
        (key,value) ->  inital.merge(key,value.intValue(),Integer::sum)
        );
System.out.println("新:"+FastJsonConvertUtils.convertObjectToJSON(inital));

结果:

新:{0:1,1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0,13:0,14:0,15:2,16:0,17:0,18:0,19:0,20:0,21:0,22:0,23:0}

list去重

list实体属性去重

  //根据name属性去重
  List<User> unique1 = list.stream().collect(
                collectingAndThen(
                   toCollection(() -> new TreeSet<>(comparing(User::getName))), ArrayList::new));
  



//根据name,age属性去重
 List<User> unique2 = list2.stream().collect(
                collectingAndThen(
                        toCollection(() -> new TreeSet<>(comparing(o -> o.getName() + ";" + o.getAge()))), ArrayList::new)
        );
      

list常规元素去重:

List uniqueList = list.stream().distinct().collect(Collectors.toList());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值