List集合按属性字段分组 List集合按某个属性或者字段进行分组的操作
有趣的灵魂_不世俗的心 人气:0想了解List集合按某个属性或者字段进行分组的操作的相关内容吗,有趣的灵魂_不世俗的心在本文为您仔细讲解List集合按属性字段分组的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:List集合,按属性分组,按字段分组,下面大家一起来学习吧。
List集合按某个属性或者字段进行分组
List<Object>分组 按照Student对象中的Institution(学院)属性进行分组统计
核心代码
Map<String, List<Student>> collect = stuList.stream().collect(Collectors.groupingBy(Student::getInstitution));
实现代码示例:
public static void main(String[] args) { List<Student> stuList=initStuList2(); Map<String, List<Student>> collect = stuList.stream().collect(Collectors.groupingBy(Student::getInstitution)); for(String key:collect.keySet()){ System.out.println(key+":" +collect.get(key).size()); System.out.println(collect.get(key)); } } public static List<Student> initStuList2(){ List<Student> stuList=new ArrayList<Student>(1000); for(int i=0;i<10;i++){ Student student = new Student(); long stu=(long) ((Math.random()*9+10000)*1000000); String Idcard=String.valueOf(stu).substring(0, 9); String ids=UUID.randomUUID().toString().replaceAll("-",""); student.setId(ids); student.setUsername("student"+i); student.setClasses("计算机"+i); student.setIdcard("362425199"+Idcard); String [] institution={"信息学院","文学院","音乐学院","体院","理学院","机电学院"}; int ss=(int)(Math.random()*6); student.setInstitution(institution[ss]); student.setMobile("18179"+Idcard.substring(0, 6)); student.setEmail(Idcard+"@qq.com"); student.setQq(Idcard); student.setHomeaddress("广东省深圳市"); student.setWeixin(Idcard); if(i%50==0){student.setSex("广东省深圳市");} else{ String[] sexs={"男","女"}; int ii=((int) Math.random()); student.setSex(sexs[ii]); } student.setCreateby("拿破仑"); student.setCreatetime(new Date()); stuList.add(student); } return stuList; }
实现效果
按照学院分组,得到体院集合中6个对象,文学院2个对象,理学院1个对象,信息学院1个对象
List<Map<String,Object>>分组统计 根据性别分类
核心代码:
Map<String, List<Map<String, Object>>> gslist = agentList.stream().collect(Collectors.groupingBy(e -> e.get("sex").toString()));
实现代码示例
public static void main(String[] args) { List<Map<String,Object>> agentList=init(); HashMap<String, Object> hashMap =(HashMap<String, Object>) agentList.get(0); Map<String, List<Map<String, Object>>> gslist = agentList.stream().collect(Collectors.groupingBy(e -> e.get("sex").toString())); for(String key:gslist.keySet()){ System.out.println(key+" : "+gslist.get(key).size()); System.out.println(gslist.get(key)); } } /*** * 初始化联系信�? * @return */ public static List<Map<String,Object>> init(){ String insertID=UUID.randomUUID().toString().replaceAll("-",""); List<Map<String,Object>> concacts= new ArrayList<Map<String,Object>>(); long time1=System.currentTimeMillis(); for(int i=0;i<10;i++){ String id=UUID.randomUUID().toString().replaceAll("-",""); Map<String, Object> map = new HashMap<String,Object>(); map.put("id", id); map.put("name", "张三"); map.put("identity_no", "36242519961225"+(int)(Math.random()*10000+1000)); map.put("telphone","1852562"+(int)(Math.random()*10000+1000)); map.put("address","江西吉安"); map.put("levels", "VIP"); map.put("source", 0); map.put("flight_no", "ZH9101"); map.put("planned_takeofftime", "1220"); map.put("estimated_takeofftime", "1425"); map.put("flight_change_type", 1); map.put("flight_change_reason", "军事活动"); map.put("flightdate","2019-05-01"); map.put("en_name", "ZHANG/SAN"); map.put("traveller_idx", (int)(Math.random()*1000+100)); String [] sexs={"男","女","同性恋","人妖"}; int kk=(int) (Math.random()*4); map.put("sex", sexs[kk]); map.put("phone_num","1302880"+(int)(Math.random()*10000+1000)); map.put("originating", "SZX"); map.put("terminus", "BKK"); map.put("ticketid", (int)(Math.random()*10000+1000)); map.put("mainspace", "J"); map.put("sonspace", "C"); map.put("message_info", "4"); map.put("extension", "1892562"+(int)(Math.random()*10000+1000)); map.put("officeid", (int)(Math.random()*10000+1000)); map.put("pnrics", (int)(Math.random()*10000+1000)); map.put("traveller_safe", "2019-02-23 ZH9007"); map.put("phone_inform", 1); concacts.add(map); } long time2=System.currentTimeMillis(); //System.out.println("初始化数据花�?"+(time2-time1)/1000); return concacts; }
实现效果
List分组的两种方式
java8之前List分组
假设有个student类,有id、name、score属性,list集合中存放所有学生信息,现在要根据学生姓名进行分组。
public Map<String, List<Student>> groupList(List<Student> students) { Map<String, List<Student>> map = new Hash<>(); for (Student student : students) { List<Student> tmpList = map.get(student.getName()); if (tmpList == null) { tmpList = new ArrayList<>(); tmpList.add(student); map.put(student.getName(), tmpList); } else { tmpList.add(student); } } return map; }
java8的List分组
public Map<String, List<Student>> groupList(List<Student> students) { Map<String, List<Student>> map = students.stream().collect(Collectors.groupingBy(Student::getName)); return map; }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
加载全部内容