您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。