• java8 Stream应用合集


    1. 多个数据集合如何合并为一个数组
    			List<Map<String,Object>> data1 = this.queryReportInvestSu(param);
    			List<Map<String,Object>> data2 = this.queryReportInvestSu(param);
    			List<Map<String,Object>> data3 = this.queryReportInvestSu(param);
    			List<Map<String,Object>> data4 = this.queryReportInvestSu(param);
                // 将所有表格的数据合并成1个数组,便于操作
                List<Map<String,Object>> dataAll = Stream.of(data1, data2, data3, data4)
                        .flatMap(Collection::stream)
                        .collect(Collectors.toList());
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. 二维数据如何拍平为一维数组, (如果你不需要去重,可以将distinct去掉)
     List<String> investorIds = investorIdArrays.stream().flatMap(Arrays::stream).distinct().collect(Collectors.toList());
    
    • 1
    1. 获取数组中每条数据中某一个属性的值
    List<Map<String,Object>> data = this.queryReportInvestSu(param);
    List<String> reviewerIdArrays =  data.stream().map((item)-> item.get("REVIEWER_ID").toString()).collect(Collectors.toList());
    
    • 1
    • 2
    1. java8 针对map中的key排序
    // 注意:这里不能用HashMap存,HashMap的遍历顺序是随机的
      public static TreeMap<String,Object> sortKeyMap(Map<String,Object> map){
    	    // Collectors.toMap()默认是HashMap
    	    TreeMap<String, Object> result = map.entrySet().stream()
    	            .sorted(Comparator.comparing(Map.Entry::getKey))
    	            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (k1, k2) -> k1, TreeMap::new));
    	    System.out.println("result: " + result);
    	    return result
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1. java8 针对map中的值排序
       // 注意:这里不能用HashMap存,HashMap的遍历顺序是随机的
      public static TreeMap<String,Object> sortValueMap(Map<String,Object> map){
           // Collectors.toMap()默认是HashMap
    	    TreeMap<String, String> result = map.entrySet().stream()
    	            .sorted(Comparator.comparing(Map.Entry::getValue))
    	            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, TreeMap::new));
    	    System.out.println("result: " + result);
    	return result
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    1. java8 按数值进行排序
    List<Map<String,Object>> list2 = list.stream().sorted((e1,e2)->{
    				if(!e1.containsKey("SUM")) {
    					e1.put("SUM", 0.00);
    				}
    				if(!e2.containsKey("SUM")) {
    					e2.put("SUM", 0.00);
    				}
    				return Double.compare((Double)e1.get("SUM"),(Double) e2.get("SUM")); //倒序在Double.compare前加一个负号‘-’
    				
    			}).collect(Collectors.toList());
    		System.out.println("map 按数值排序:"+list2);
    
    
    //		对实例类进行排序
    		List<Employee> emps = Arrays.asList(
             new Employee("啊三",18,999.99,Status.BUSY),
    			new Employee("张四",36,9992341.99,Status.FREE),
    			new Employee("六五",28213,9199.99,Status.FREE),
    			new Employee("欧弟",183121,99.2,Status.VOCATION),
    			new Employee("秋七",823,10.3,Status.FREE),
    			new Employee("绿七",82,10.1,Status.VOCATION),
    			new Employee("张七",82,10.2,Status.BUSY)
    			);
    		System.out.println("实体类 排序前的值 : "+emps);
    		List<Employee> emps2 = emps.stream().sorted((e1,e2)-> Double.compare((Double)e1.getSalary(),(Double) e2.getSalary())).collect(Collectors.toList());
    		System.out.println("实体类按数值排序" + emps2);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    1. java8 按照中文排序
    		//按中文排序
    		List<Map<String,Object>> list4 = list.stream().sorted((e1,e2)->{
    			Collator collator = Collator.getInstance(Locale.CHINA);
    			return collator.compare(e1.get("NAME"), e2.get("NAME"));	
    		}).collect(Collectors.toList());
    	    System.out.println("map 按中文排序" + list4);  
    		// 中文排序倒序(只需要将compare的参数换一下即可,实体类中文倒序也是一样的)
    		List<Map<String,Object>> list5 = list.stream().sorted((e1,e2)->{
      			Collator collator = Collator.getInstance(Locale.CHINA);
      			return collator.compare(e2.get("NAME"),e1.get("NAME"));	
      		}).collect(Collectors.toList());
      	    System.out.println("map 按中文倒序" + list5); 
    
    //实体类 按中文排序
    		List<Employee> emps3 = emps.stream().sorted((e1,e2)->{
    			Collator collator = Collator.getInstance(Locale.CHINA);
    			return collator.compare(e1.getName(), e2.getName());	
    		}).collect(Collectors.toList());
    	    System.out.println("实体类按中文排序" + emps3);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    1. java8 根据集合中的某个属性进行分组
    package java8分组之后的数据和之前的顺序保持一致;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.LinkedHashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;
    
    public class Dome {
    	public static void main(String[] args) {
    		List<Map<String,Object>> list = new ArrayList<>();
    		Map<String,Object> map1 = new HashMap<>();
    		map1.put("key1", "aa");
    		map1.put("key2", "bb");
    		map1.put("key3", "cc");
    		map1.put("key4", "dd");
    		Map<String,Object> map2 = new HashMap<>();
    		map2.put("key1", "aa");
    		map2.put("key2", "bb");
    		map2.put("key3", "cc");
    		map2.put("key4", "dd");
    		Map<String,Object> map3 = new HashMap<>();
    		map3.put("key1", "aa");
    		map3.put("key2", "bb1");
    		map3.put("key3", "cc");
    		map3.put("key4", "dd");
    		Map<String,Object> map4 = new HashMap<>();
    		map4.put("key1", "aa");
    		map4.put("key2", "bb1");
    		map4.put("key3", "cc");
    		map4.put("key4", "dd");
    		Map<String,Object> map5 = new HashMap<>();
    		map5.put("key1", "aa");
    		map5.put("key2", "bb3");
    		map5.put("key3", "cc");
    		map5.put("key4", "dd");
    		list.add(map1);
    		list.add(map2);
    		list.add(map3);
    		list.add(map4);
    		list.add(map5);
    		System.out.println(list);
    		/**
    		 * 源数据
    		 * [{key1=aa, key2=bb, key3=cc, key4=dd},
    		 *  {key1=aa, key2=bb, key3=cc, key4=dd},
    		 *   {key1=aa, key2=bb1, key3=cc, key4=dd},
    		 *    {key1=aa, key2=bb1, key3=cc, key4=dd}, 
    		 *    {key1=aa, key2=bb3, key3=cc, key4=dd}]
    
    		 */
    		
    		//无序分组,Collectors.groupingBy(e -> e.get("key2")
    		Map<Object,List<Map<String,Object>>> group = list.stream().collect(Collectors.groupingBy(e -> e.get("key2")));
    		System.out.println(group);
    		/*
    		 * {bb1=[{key1=aa, key2=bb1, key3=cc, key4=dd}, {key1=aa, key2=bb1, key3=cc, key4=dd}], 
    		 *  bb=[{key1=aa, key2=bb, key3=cc, key4=dd}, {key1=aa, key2=bb, key3=cc, key4=dd}],
    		 *  bb3=[{key1=aa, key2=bb3, key3=cc, key4=dd}]}
    		 */
    		// 有序分组 只需要在加两个参数Collectors.groupingBy(e -> e.get("key2"),LinkedHashMap::new,Collectors.toList())
    		Map<Object,List<Map<String,Object>>> group2 = list.stream().collect(Collectors.groupingBy(e -> e.get("key2"),LinkedHashMap::new,Collectors.toList()));
    		System.out.println(group2);
    		/**
    		 * {bb=[{key1=aa, key2=bb, key3=cc, key4=dd}, {key1=aa, key2=bb, key3=cc, key4=dd}], 
    		 *  bb1=[{key1=aa, key2=bb1, key3=cc, key4=dd}, {key1=aa, key2=bb1, key3=cc, key4=dd}], 
    		 *  bb3=[{key1=aa, key2=bb3, key3=cc, key4=dd}]}
    		 */
    	
    	
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    1. java8 对集合中某个属性计算出合计值
    IntSummaryStatistics sumcc =  slist.stream().collect(Collectors.summarizingInt(e->Integer.valueOf(e.get("规模").toString())));
    nmap.put("部门", sumcc.getSum());//求和
    
    • 1
    • 2
    1. java8 数组按照条件过滤
        List<Map<String, Object>> finalData1  = data.stream().filter((item)-> finalData1.get(finalI).get(Constant.RULE_TYPE).equals(item.get(Constant.RULE_TYPE)) && !Constant.SYMBOL.equals(item.get(Constant.SEC_TYPE1))).collect(Collectors.toList())
    
    
    • 1
    • 2
  • 相关阅读:
    大学英语四级考试核心高频词汇突破
    logback 日志,java-jar 启动报错
    【精讲】vue中的添加数据、监测数据(data)原理及拓展、监测数组及(对象,数组)监测概括
    JVM内存模型介绍
    10_18Qt
    Python3数据科学包系列(二):数据分析实战
    基于粒子群算法训练常规自动编码器附Matlab代码
    Vue 3 Teleport:掌控渲染的艺术
    Python基础之输入输出
    测试开发环境下centos7.9下安装docker的minio
  • 原文地址:https://blog.csdn.net/weixin_43865196/article/details/132736464