Java8对List中的对象同一标识分组,和分集合对象,分单

Java8对List中的对象同一标识分组,和分集合对象,分单

import lombok.Data;

@Data
public class TestPanVo {

    private String id;

    private String address;


}
  • TestPanVo是对象的。这个是通过ID来分组,相同的标识为一组

import java.util.*;

public class Test12 {

    public static void main(String[] args) {

        List<TestPanVo> entityList = new ArrayList<>();

        TestPanVo testPanVo = new TestPanVo();
        testPanVo.setId("11");
        testPanVo.setAddress("广东广州大学的具体地址是在广东省广州市大学城外环西路230号");

        TestPanVo testPanVo1 = new TestPanVo();
        testPanVo1.setId("11");
        testPanVo1.setAddress("广东省广州市大学城外环西路230号");

        TestPanVo testPanVo2 = new TestPanVo();
        testPanVo2.setId("22");
        testPanVo2.setAddress("深圳人才大市场总部地址:罗湖区宝安北路人才大市场大厦");

        TestPanVo testPanVo3 = new TestPanVo();
        testPanVo3.setId("22");
        testPanVo3.setAddress("罗湖区宝安北路人才大市场大厦");

        TestPanVo testPanVo4 = new TestPanVo();
        testPanVo4.setId("33");
        testPanVo4.setAddress("广东省珠海市卫星地图浏览,珠海市卫星地图-广东省珠海市、区、县、村各级地图浏览");

        TestPanVo testPanVo5 = new TestPanVo();
        testPanVo5.setId("33");
        testPanVo5.setAddress("珠海市卫星地图-广东省珠海市、区、县、村各级地图浏览");

        TestPanVo testPanVo6 = new TestPanVo();
        testPanVo6.setId("44");
        testPanVo6.setAddress("清远市清城区,新站电话是:暂无电话,新站简介:广东省清远市清城区邮编:511500,新站周边二手房、租房、购物广场、休闲娱乐、美容健身...");


        entityList.add(testPanVo);
        entityList.add(testPanVo1);
        entityList.add(testPanVo2);
        entityList.add(testPanVo3);
        entityList.add(testPanVo4);
        entityList.add(testPanVo5);
        entityList.add(testPanVo6);

        List<List<TestPanVo>> listByGroup = getListByGroup(entityList);
        System.out.println(listByGroup);
        System.out.println("********************************");

        System.out.println(listByGroup.get(0));
        System.out.println("********************************");
        System.out.println(listByGroup.get(1));
        System.out.println("********************************");
        System.out.println(listByGroup.get(2));
        System.out.println("********************************");
        System.out.println(listByGroup.get(3));
        System.out.println("********************************");



    }


    /**
     *这个是使用某个地方进行单据的分组,通过某个标识不同进行分组处理;
     */

    public  static List<List<TestPanVo>> getListByGroup(Collection<TestPanVo> list) {                            //这个是一个方法;
        List<List<TestPanVo>> result = new ArrayList<List<TestPanVo>>();
        Map<String, List<TestPanVo>> map = new TreeMap<String, List<TestPanVo>>();

        for (TestPanVo bean : list) {                                                                                //这个是进行遍历分组;
            if (map.containsKey(bean.getId())) {
                List<TestPanVo> t = map.get(bean.getId());
                t.add(bean);
                map.put(bean.getId(), t);
            } else {
                List<TestPanVo> t = new ArrayList<>();
                t.add(bean);
                map.put(bean.getId(), t);
            }
        }
        for (Map.Entry<String, List<TestPanVo>> entry : map.entrySet()) {
            result.add(entry.getValue());
        }
        return result;
    }
}

重点方法

  • TestPanVo是对象的。这个是通过ID来分组,相同的标识为一组
   /**
     *这个是使用某个地方进行单据的分组,通过某个标识不同进行分组处理;
     */

    public  static List<List<TestPanVo>> getListByGroup(Collection<TestPanVo> list) {                            //这个是一个方法;
        List<List<TestPanVo>> result = new ArrayList<List<TestPanVo>>();
        Map<String, List<TestPanVo>> map = new TreeMap<String, List<TestPanVo>>();

        for (TestPanVo bean : list) {                                                                                //这个是进行遍历分组;
            if (map.containsKey(bean.getId())) {
                List<TestPanVo> t = map.get(bean.getId());
                t.add(bean);
                map.put(bean.getId(), t);
            } else {
                List<TestPanVo> t = new ArrayList<>();
                t.add(bean);
                map.put(bean.getId(), t);
            }
        }
        for (Map.Entry<String, List<TestPanVo>> entry : map.entrySet()) {
            result.add(entry.getValue());
        }
        return result;
    }
  • 运行结果集
    结果说明
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值