List集合按某个属性或者字段进行分组
1.List分组
List里面的对象元素,以某个属性来分组,例如,以id分组,将id相同的放在一起
1.1对象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, num=30}], 3=[Apple{id=3, name='荔枝', money=9.99, num=40}]}
1.2 Map List分组
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;
}
2.过滤filter
从集合中过滤出来符合条件的元素:
//过滤出符合条件的数据
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}]
3.求和
将集合中的数据按照某个属性求和:
//计算 总金额
BigDecimal totalMoney = appleList.stream().map(Apple::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
System.err.println("totalMoney:"+totalMoney); //totalMoney:17.48
4.排序
按照某一属性排序
List<Map<String, Object>> executorList = getExecutorList();
executorList.sort(Comparator.comparingInt(x -> Integer.parseInt(x.get("sortnumber").toString())));
5.总结对map做统计的操作
计数
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())));