Java8的stream常用的操作

记录一下常用的用法

定义测试对象

java 复制代码
	@Data
	public class Employee {
	    //id
	    private Integer id;
	    //姓名
	    private String name;
	    //年龄
	    private Integer age;
	    //身高
	    private Double height;
	    //存款
	    private BigDecimal deposit;
	
	    public Employee(Integer id, String name, Integer age, Double height, BigDecimal deposit) {
	        this.id = id;
	        this.name = name;
	        this.age = age;
	        this.height = height;
	        this.deposit = deposit;
	    }
    }
  1. list操作如排序、求和、获取最大最小值、累加等

    java 复制代码
      List<Employee> listOne = new ArrayList<>();
            listOne.add(new Employee(1, "张三", 12, 160.23, new BigDecimal("5000")));
            listOne.add(new Employee(2, "李四", 18, 182.56, new BigDecimal("2000.15")));
            listOne.add(new Employee(3, "王五", 15, 185.65, new BigDecimal("300")));
            listOne.add(new Employee(4, "周扒皮", 55, 165.55, new BigDecimal("180654.57")));
            listOne.add(new Employee(5, "独眼龙", 18, 195.55, new BigDecimal("154.12")));
    
            //根据年龄从小到大排序 升序
            List<Employee> ages1 = listOne.stream()
                    .sorted(Comparator.comparing(Employee::getAge))
                    .collect(Collectors.toList());
            ages1.forEach(System.out::println);
            System.out.println("---------------------------------------------------------");
            //根据年龄从大到小排序 降序
            List<Employee> ages2 = listOne.stream()
                    .sorted(Comparator.comparing(Employee::getAge, Comparator.reverseOrder()))
                    .collect(Collectors.toList());
            ages2.forEach(System.out::println);
            System.out.println("---------------------------------------------------------");
            //根据年龄从大到小排序 降序  根据身高从大到小排序 降序(仅针对于当一个属性相等时才有效)
            List<Employee> ages3 = listOne.stream()
                    .sorted(Comparator.comparing(Employee::getAge, Comparator.reverseOrder())
                            .thenComparing(Employee::getHeight, Comparator.reverseOrder()))
                    .collect(Collectors.toList());
            ages3.forEach(System.out::println);
            System.out.println("---------------------------------------------------------");
    
            //获取年龄之和
            int ages = listOne.stream().mapToInt(Employee::getAge).sum();
            //获取身高之和
            double heights = listOne.stream().mapToDouble(Employee::getHeight).sum();
            //全部的存款
            BigDecimal reduce = listOne.stream().map(Employee::getDeposit).reduce(BigDecimal.ZERO, BigDecimal::add);
  2. list转map

    java 复制代码
      //根据id转map Employee::getId作为key的属性 e作为map的value  (v1,v2)->v2 当key不唯一时的操作
            Map<Integer, Employee> map1 = listOne.stream().collect(Collectors.toMap(Employee::getId, e -> e, (v1, v2) -> v2));
            //根据某个属性(年龄)分组
            Map<Integer, List<Employee>> map2 = listOne.stream().collect(Collectors.groupingBy(Employee::getAge));
  3. 两个map合并

    java 复制代码
     	Map<Integer, Employee> map3 = new HashMap<>();
        map3.put(2, new Employee(2, "李四2", 20, 182.56, new BigDecimal("2000.15")));
        map3.put(3, new Employee(3, "王五2", 16, 182.99, new BigDecimal("2222.15")));
        map3.put(4, new Employee(4, "周扒皮2", 60, 164.55, new BigDecimal("154.57")));
        map3.put(6, new Employee(6, "赵六", 18, 182.56, new BigDecimal("2000.15")));
    
        //合并 (v1, v2)key相同时value的值 v1为map3的value v2为map1的value
        map1.forEach((key1, value1) -> map3.merge(key1, value1, (v1, v2) -> {
            System.out.println("v1:"+v1);
            System.out.println("v2:"+v2);
            v2.setName(v1.getName());
            v2.setDeposit(v1.getDeposit().add(v2.getDeposit()));
            return v2;
        }));
        System.out.println("---------------------------------------------------------");
        map3.forEach((key,value)-> System.out.println(key+":"+value));
  4. 结果

    java 复制代码
    Employee(id=1, name=张三, age=12, height=160.23, deposit=5000)
    Employee(id=3, name=王五, age=15, height=185.65, deposit=300)
    Employee(id=2, name=李四, age=18, height=182.56, deposit=2000.15)
    Employee(id=5, name=独眼龙, age=18, height=195.55, deposit=154.12)
    Employee(id=4, name=周扒皮, age=55, height=165.55, deposit=180654.57)
    ---------------------------------------------------------
    Employee(id=4, name=周扒皮, age=55, height=165.55, deposit=180654.57)
    Employee(id=2, name=李四, age=18, height=182.56, deposit=2000.15)
    Employee(id=5, name=独眼龙, age=18, height=195.55, deposit=154.12)
    Employee(id=3, name=王五, age=15, height=185.65, deposit=300)
    Employee(id=1, name=张三, age=12, height=160.23, deposit=5000)
    ---------------------------------------------------------
    Employee(id=4, name=周扒皮, age=55, height=165.55, deposit=180654.57)
    Employee(id=5, name=独眼龙, age=18, height=195.55, deposit=154.12)
    Employee(id=2, name=李四, age=18, height=182.56, deposit=2000.15)
    Employee(id=3, name=王五, age=15, height=185.65, deposit=300)
    Employee(id=1, name=张三, age=12, height=160.23, deposit=5000)
    ---------------------------------------------------------
    v1:Employee(id=2, name=李四2, age=20, height=182.56, deposit=2000.15)
    v2:Employee(id=2, name=李四, age=18, height=182.56, deposit=2000.15)
    v1:Employee(id=3, name=王五2, age=16, height=182.99, deposit=2222.15)
    v2:Employee(id=3, name=王五, age=15, height=185.65, deposit=300)
    v1:Employee(id=4, name=周扒皮2, age=60, height=164.55, deposit=154.57)
    v2:Employee(id=4, name=周扒皮, age=55, height=165.55, deposit=180654.57)
    ---------------------------------------------------------
    1:Employee(id=1, name=张三, age=12, height=160.23, deposit=5000)
    2:Employee(id=2, name=李四2, age=18, height=182.56, deposit=4000.30)
    3:Employee(id=3, name=王五2, age=15, height=185.65, deposit=2522.15)
    4:Employee(id=4, name=周扒皮2, age=55, height=165.55, deposit=180809.14)
    5:Employee(id=5, name=独眼龙, age=18, height=195.55, deposit=154.12)
    6:Employee(id=6, name=赵六, age=18, height=182.56, deposit=2000.15)
相关推荐
喵叔哟1 分钟前
重构代码之移动字段
java·数据库·重构
喵叔哟1 分钟前
重构代码之取消临时字段
java·前端·重构
fa_lsyk4 分钟前
maven环境搭建
java·maven
Daniel 大东23 分钟前
idea 解决缓存损坏问题
java·缓存·intellij-idea
wind瑞29 分钟前
IntelliJ IDEA插件开发-代码补全插件入门开发
java·ide·intellij-idea
HappyAcmen29 分钟前
IDEA部署AI代写插件
java·人工智能·intellij-idea
马剑威(威哥爱编程)35 分钟前
读写锁分离设计模式详解
java·设计模式·java-ee
鸽鸽程序猿35 分钟前
【算法】【优选算法】前缀和(上)
java·算法·前缀和
修道-032336 分钟前
【JAVA】二、设计模式之策略模式
java·设计模式·策略模式
九圣残炎42 分钟前
【从零开始的LeetCode-算法】2559. 统计范围内的元音字符串数
java·算法·leetcode