java 8 ????????????Stream?????????/??????

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 8 ????????????Stream?????????/??????相关的知识,希望对你有一定的参考价值。

?????????reac   ??????   ring   ted   Collector   col   equals   ?????????   strong   

 

Stream??????

Stream???Java8?????????????????????API????????????java.util.stream?????????Stream API?????????????????????????????????Java?????????????????????????????????????????????????????????Java?????????????????????????????????????????????????????????????????????????????????
????????????????????????????????????????????? ???????????????????????? ???????????????????????????????????????????????? ??????????????? ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

Stream??????

  • ????????????
class Employee {
    private Long empno; //?????????
    private String ename; //????????????
    private Integer salary; //??????
    private Integer deptno; //???????????????
    //????????????get/set???????????????????????????toString??????
}
Employee e1 = new Employee(7369L, "SMITH", 800, 20);
Employee e2 = new Employee(7499L, "ALLEN", 1600, 30);
Employee e3 = new Employee(7521L, "WARD", 1250, 30);
Employee e4 = new Employee(7782L, "CLARK", 2450, 10);
Employee e5 = new Employee(7876L, "ADAMS", 1100, 20);

List<Employee> employees = Arrays.asList(e1, e2, e3, e4, e5);
  • forEach??????
    forEach??????????????????stream????????????????????????
employees.stream().forEach(System.out::println);

???????????????

Employee{empno=7369, ename=???SMITH???, salary=800, deptno=20}
Employee{empno=7499, ename=???ALLEN???, salary=1600, deptno=30}
Employee{empno=7521, ename=???WARD???, salary=1250, deptno=30}
Employee{empno=7782, ename=???CLARK???, salary=2450, deptno=10}
Employee{empno=7876, ename=???ADAMS???, salary=1100, deptno=20}
  • map??????
    map???????????????????????????????????????stream????????????????????????????????????
//???????????????????????????
List<String> enames = employees.stream().map(employee -> employee.getEname()).collect(Collectors.toList());
enames.stream().forEach(System.out::println);

???????????????

SMITH
ALLEN
WARD
CLARK
ADAMS
  • mapToInt/mapToLong/mapToDouble??????
    ??????????????????????????????stream??????????????????????????????????????????
 //?????????????????????????????????
int totalSalary = employees.stream().mapToInt(employee -> employee.getSalary()).sum();
System.out.println("???????????????" + totalSalary);

???????????????

???????????????7200
  • filter??????
    filter????????????????????????????????????stream??????????????????????????????
//??????????????????1500?????????
List<Employee> filterEmp = employees.stream().filter(employee -> employee.getSalary()>1500).collect(Collectors.toList());
filterEmp.stream().forEach(System.out::println);

???????????????

Employee{empno=7499, ename=???ALLEN???, salary=1600, deptno=30}
Employee{empno=7782, ename=???CLARK???, salary=2450, deptno=10}
  • sorted??????
    sorted??????????????????????????????????????????
//????????????????????????????????????
List<Employee> sortedEmp = employees.stream().sorted(Comparator.comparing(Employee::getSalary)).collect(Collectors.toList());
sortedEmp.stream().forEach(System.out::println);

???????????????

Employee{empno=7369, ename=???SMITH???, salary=800, deptno=20}
Employee{empno=7876, ename=???ADAMS???, salary=1100, deptno=20}
Employee{empno=7521, ename=???WARD???, salary=1250, deptno=30}
Employee{empno=7499, ename=???ALLEN???, salary=1600, deptno=30}
Employee{empno=7782, ename=???CLARK???, salary=2450, deptno=10}
  • Collectors???
    Collectors ??????????????????????????????????????????????????????????????????????????????Collectors ?????????????????????????????????
//????????????????????????????????????
Map<Integer, List<Employee>> map = employees.stream().collect(Collectors.groupingBy(employee -> employee.getDeptno()));
for(Map.Entry<Integer, List<Employee>> entry : map.entrySet()) {
    System.out.println("key: " + entry.getKey() + "  value???" + entry.getValue());
}

System.out.println();

//????????????????????????","????????????
String enameString = employees.stream().map(employee -> employee.getEname()).collect(Collectors.joining(","));
System.out.println(enameString);

???????????????

key: 20  value???[Employee{empno=7369, ename=???SMITH???, salary=800, deptno=20}, Employee{empno=7876, ename=???ADAMS???, salary=1100, deptno=20}]
key: 10  value???[Employee{empno=7782, ename=???CLARK???, salary=2450, deptno=10}]
key: 30  value???[Employee{empno=7499, ename=???ALLEN???, salary=1600, deptno=30}, Employee{empno=7521, ename=???WARD???, salary=1250, deptno=30}]

SMITH,ALLEN,WARD,CLARK,ADAMS
  • ????????????
    Stream API???????????????????????????????????????????????????????????????
//??????20?????????????????????,???????????????????????????
List<String> names = employees.stream().filter(employee -> employee.getDeptno().equals(20)).sorted(Comparator.comparing(Employee::getSalary).reversed()).map(employee -> employee.getEname()).collect(Collectors.toList());
names.stream().forEach(System.out::println);

???????????????

ADAMS
SMITH
    • ??????
      Stream API?????????????????????????????????????????????????????????????????????????????????Java7?????????????????????????????????????????????Stream API???????????????????????????????????????









以上是关于java 8 ????????????Stream?????????/??????的主要内容,如果未能解决你的问题,请参考以下文章

Java 8 Stream

java 8 Stream 代码清单+API介绍

jdk1.8 java.util.stream.Stream类 详解

Java 8 Stream实践

Java 8 Stream 用法

Java 8 Stream 流详解