List分组-对象List分组-MapList分组List中的对象元素以某个属性分组。例如,用id分组,将id放在一起//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}}List中的Map元素,以某种属性分组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; } 过滤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}按属性求和集合中的数据://计算 BigDecimal总金额 totalMoney = appleList.stream().map(Apple::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);System.err.println("totalMoney:"+totalMoney); //totalMoney:17.48根据某一属性排序Listtt<Map<String, Object>> executorList = getExecutorList();executorList.sort(Comparator.comparingInt(x -> Integer.parseInt(x.get("sortnumber").toString())));
