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

tech2025-10-02  2

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)