个人笔记,仅供参考。
是数据渠道,用于操作数据源(集合、数组等)所生成的元素序列。
“集合讲的是数据,Stream讲的是计算!”
注意:
public class EmployeeData {
public static List<Employee> getEmployees(){
List<Employee> list = new ArrayList<>();
list.add(new Employee(1001, "马化腾", 34, 6000.38));
list.add(new Employee(1002, "马云", 12, 9876.12));
list.add(new Employee(1003, "刘强东", 33, 3000.82));
list.add(new Employee(1004, "雷军", 26, 7657.37));
list.add(new Employee(1005, "李彦宏", 65, 5555.32));
list.add(new Employee(1006, "比尔盖茨", 42, 9500.43));
list.add(new Employee(1007, "任正非", 26, 4333.32));
list.add(new Employee(1008, "扎克伯格", 35, 2500.32));
return list;
}
}
public class Employee {
private int id;
private String name;
private int age;
private double salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Employee() {
}
public Employee(int id) {
this.id = id;
}
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public Employee(int id, String name, int age, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", salary=" + salary + '}';
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Employee employee = (Employee) o;
if (id != employee.id)
return false;
if (age != employee.age)
return false;
if (Double.compare(employee.salary, salary) != 0)
return false;
return name != null ? name.equals(employee.name) : employee.name == null;
}
@Override
public int hashCode() {
int result;
long temp;
result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + age;
temp = Double.doubleToLongBits(salary);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
}
/*1、通过集合*/
@Test
public void test1(){
List<Employee> employeeList = new ArrayList<>();
/* 创建顺序流:default Stream stream; */
Stream<Employee> stream = employeeList.stream();
/* 创建并行流:default Stream parallelStream(); */
Stream<Employee> parallelStream = employeeList.parallelStream();
}
/*2、通过数组*/
@Test
public void test2(){
/*static Stream stream(T[] array): 返回一个流*/
Integer[] arr = {1,2,1,2,3,5};
Stream<Integer> stream = Arrays.stream(arr);
int[] intArr = {1,2,5,8,5,2};
IntStream stream1 = Arrays.stream(intArr);
}
/*3、通过Stream的of()*/
@Test
public void tes3(){
/*public static Stream of(T... values) : 返回一个流*/
Stream<String> stream = Stream.of("AA", "BB", "CC");
}
/*4、创建无限流*/
@Test
public void test4(){
/* 迭代
public static Stream iterate(final T seed, final UnaryOperator f)*/
/* 生成
public static Stream generate(Supplier s)*/
}
/* 一、筛选与切片*/
@Test
public void test1(){
List<Employee> employeeList = EmployeeData.getEmployees();
Stream<Employee> stream = employeeList.stream();
/* 1、filter(Predicate P),接收lambda,从流中过滤出符合条件的数据。
filter查询出员工中工资大于7000的员工
*/
stream.filter(employee -> employee.getSalary() > 7000 ).forEach(System.out::println);
System.out.println();
/* 2、limit(n)-截断流,使其元素不超过给定数量
查询工资大于7000的前两条数据
*/
employeeList.stream().filter(employee -> employee.getSalary() > 7000 ).limit(2).forEach(System.out::println);
System.out.println();
/* 3、skip(n)-截断流,跳过前n个数据
查询工资大于7000的前两条数据之外的数据
*/
employeeList.stream().filter(employee -> employee.getSalary() > 7000 ).skip(2).forEach(System.out::println);
/*
* 4、distinct()-筛选,通过流生成的hashcode和equals()
根据姓名去重
*/
employeeList.add(new Employee(1006, "比尔盖茨", 42, 9500.43));
employeeList.add(new Employee(1007, "任正非", 26, 4333.32));
employeeList.add(new Employee(1008, "扎克伯格", 35, 2500.32));
employeeList.stream().distinct().forEach(System.out::println);
}
/* 二、映射*/
@Test
public void test(){
List<String> list = Arrays.asList("A", "BB", "CCC");
/* 1、map(Function f):接受一个函数作为参数,将元素转换成其他形式或提取信息,该函数会被应用到每一个元素上
将集合中的全部元素转换成小写
*/
list.stream().map(String::toLowerCase).forEach(System.out::println);
list.stream().map(str -> str.toLowerCase()).forEach(System.out::println);
System.out.println();
/*
* 获取员工中的员工名称长度大于三的员工
*/
List<Employee> employeeList = EmployeeData.getEmployees();
employeeList.stream().filter(emp -> emp.getName().length() > 3).forEach(System.out::println);
/*
* 获取员工中的员工名称长度大于三的员工姓名
*/
employeeList.stream().filter(emp -> emp.getName().length() > 3).map(emp -> emp.getName()).forEach(System.out::println);
System.out.println();
employeeList.stream().map(emp -> emp.getName()).filter(emp -> emp.length() > 3).forEach(System.out::println);
System.out.println();
employeeList.stream().map(Employee::getName).filter(emp -> emp.length() > 3).forEach(System.out::println);
}
/* 三、排序*/
@Test
public void test3(){
Integer[] intArr = {22,5,6,8,52,54,63};
String[] strArr = {"AA","JJ","BB","GG"};
/* sorted()--自然排序*/
Arrays.stream(intArr).sorted().forEach(System.out::println);
Arrays.stream(strArr).sorted().forEach(System.out::println);
/* sorted(Comparator com)--定制排序*/
List<Employee> employeeList = EmployeeData.getEmployees();
employeeList.stream().sorted((e1,e2) -> (int) (e1.getSalary() - e2.getSalary())).forEach(System.out::println);
Arrays.stream(intArr).sorted((s1, s2) -> -s1.compareTo(s2)).forEach(System.out::println);
}
/*一、匹配与查找*/
@Test
public void test1(){
List<Employee> employeeList = EmployeeData.getEmployees();
// allMatch(Predicate p) 是否所有员工的年龄都大于30
System.out.println(employeeList.stream().allMatch(e -> e.getAge() > 30));
// anyMatch(Predicate p) 是否存在员工年龄大于40的员工
System.out.println(employeeList.stream().anyMatch(emp -> emp.getAge() > 40));
// 是否有员工工资大于一万的数据
System.out.println(employeeList.stream().anyMatch(emp -> emp.getSalary() > 10000));
// findFirst(Predicate p) 返回符合条件的第一个元素
System.out.println(employeeList.stream().filter(emp -> emp.getName().length() > 3).findFirst().get());
}
@Test
public void test2(){
List<Employee> employeeList = EmployeeData.getEmployees();
// count() 返回流中元素的总个数
// 统计工资大于7000的员工的个数
System.out.println(employeeList.stream().filter(emp -> emp.getSalary() > 7000).count());
// max(Comparator com) 返回流中元素最大值
// 返回流中最高的工资
System.out.println(employeeList.stream().map(employee -> employee.getSalary()).max(Double::compare).get());
// min(Comparator com) 返回流中元素最小值
// 返回流中最低的工资的员工信息
System.out.println(employeeList.stream().min(Comparator.comparingDouble(Employee::getSalary)).get());
//forEach(Consumer c) 内部迭代
// employeeList.stream().forEach(System.out::println);
// 针对于集合java 8 增加了forEach()方法
employeeList.forEach(System.out::println);
}
/* 二、归约 */
@Test
public void test3(){
List<Employee> employeeList = EmployeeData.getEmployees();
// reduce(T identity,BinaryOperator) - 将流中的元素反复结合起来得到一个新值,返回T
// 计算1-10的自然数之和
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
System.out.println(list.stream().reduce(0, (x, y) -> x + y));
System.out.println(list.stream().reduce(0, (x, y) -> Integer.sum(x, y)));
System.out.println(list.stream().reduce(0, Integer::sum));
// reduce(BinaryOperator) - 将流中的元素反复结合起来得到一个新值,返回Optional
// 计算所有员工的工资之和
System.out.println(employeeList.stream().map(Employee::getSalary).reduce(0.0, (x, y) -> x + y));
System.out.println(employeeList.stream().map(Employee::getSalary).reduce(0.0, (x, y) -> Double.sum(x, y)));
System.out.println(employeeList.stream().map(Employee::getSalary).reduce(0.0, Double::sum));
}
/* 三、收集 */
@Test
public void test4(){
List<Employee> employeeList = EmployeeData.getEmployees();
// collect(Collect c) 将流转换为其他形式。接收一个Collector接口的实现,用于给Stream流中元素做汇总的方法
// 查找工资大于6000的员工,并返回一个新的list或者set
System.out.println(employeeList.stream().filter(emp -> emp.getSalary() > 70000).collect(Collectors.toList()));
Set<Employee> set = employeeList.stream().filter(emp -> emp.getSalary() > 7000).collect(Collectors.toSet());
set.stream().forEach(System.out::println);
// 按照员工的年龄进行排序,返回到一个新的list中
System.out.println(employeeList.stream().sorted((e1, e2) -> (int) (e1.getSalary() - e2.getSalary())).collect(Collectors.toList()));
}