记录一下常用的用法
定义测试对象
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;
}
}
-
list操作如排序、求和、获取最大最小值、累加等
javaList<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);
-
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));
-
两个map合并
javaMap<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));
-
结果
javaEmployee(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)