List 集合按某个属性或者字段进⾏分组
List 集合按某个属性或者字段进⾏分组
1.List 分组
List⾥⾯的对象元素,以某个属性来分组,例如,以id分组,将id相同的放在⼀起
1.1对象List 分组
1.2 Map List 分组//List
以ID 分组 Map<Integer,List<Apple>>
Map <Integer , List <Apple >> groupBy = appleList .stream ().collect (Collectors .groupingBy (Apple ::getId ));
System .err .println ("groupBy:"+groupBy );
{1=[Apple {id =1, name ='苹果1', money =3.25, num =10}, Apple {id =1, name ='苹果2', money =1.35, num =20}], 2=[Apple {id =2, name ='⾹蕉', money =2.89
List<Map<String,Object>> agentList=init();
HashMap<String, Object> hashMap =(HashMap<String, Object>) (0);
Map<String, List<Map<String, Object>>> gslist = agentList.stream().upingBy(e -> e.get("sex").toString()));
for(String key:gslist.keySet()){
System.out.println(key+" : "+(key).size());
System.out.(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));
groupby分组
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;
}
2.过滤filter
从集合中过滤出来符合条件的元素:
3.求和
将集合中的数据按照某个属性求和:
4.排序
按照某⼀属性排序
5.总结对map 做统计的操作
计数
排序(逆序)
累加求和
分组:
//过滤出符合条件的数据
List <Apple > filterList = appleList .stream ().filter (a -> a .getName ().equals ("⾹蕉")).collect (Collectors .toList ());
System .err .println ("filterList:"+filterList
);[Apple {id =2, name ='⾹蕉', money =2.89, num =30}]
//计算 总⾦额
BigDecimal totalMoney = appleList .stream ().map (Apple ::getMoney ).
reduce (BigDecimal .ZERO , BigDecimal ::add );System .err .println ("totalMoney:"+totalMoney );  //totalMoney:17.48
List <Map <String ,
Object >> executorList = getExecutorList ();
executorList .sort (Comparator paringInt (x -> Integer .parseInt (x .get ("sortnumber").toString ())));
Map <String , Long > map = AnimalList .stream ().
collect (Collectors .groupingBy (Animal ::getName ,Collectors .counting ()));
map .entrySet
().stream ().sorted (Map .Entry .<String , Long >comparingByValue ().reversed ())
.forEachOrdered (System .out ::println );
Map <String , Integer > sumMap = AnimalList .stream ().
collect .
(Collectors .groupingBy (Animal ::getName , Collectors .summingInt (Animal ::getPrice )));
Map <String , List <Integer >> groupMap =
AnimalList .stream ().collect (Collectors .groupingBy (Animal ::getName ,
Collectors .mapping (Animal ::getPrice , Collectors .toList ())));

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。