这篇文章给大家分享的是有关List集合按某个属性或者字段进行分组的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
核心代码
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个对象
核心代码:
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; }
实现效果
假设有个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; }
public Map<String, List<Student>> groupList(List<Student> students) { Map<String, List<Student>> map = students.stream().collect(Collectors.groupingBy(Student::getName)); return map; }
感谢各位的阅读!关于“List集合按某个属性或者字段进行分组的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。