学习笔记:Stream流的创建、中间以及终端操作
一、Stream流的创建
??惰性操作,每当访问到流中的一个元素,才在此元素上执行相关操作,Stream不保存数据,故每个Stream流只能使用一次,分为中间操作和终止操作,中间操作:返回结果为Stream,可有多个中间操作,终止操作:返回数据,只能有一个。
* Stream的实例化
* 创建方式:
* 方式一:通过集合创建
* 方式二:通过数组
* 方式三:通过Stream的of()、iterate()、generate()
@Test
public void test1(){
List<Employee> employees = EmployeeData.getEmployees();
Stream<Employee> stream = employees.stream();
Stream<Employee> employeeStream = employees.parallelStream();
}
@Test
public void test2(){
int[] arr = new int[]{1,2,3,4,5};
IntStream stream = Arrays.stream(arr);
System.out.println(stream);
}
@Test
public void test3(){
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
Stream<Integer> stream1 = Stream.iterate(0, x -> x + 3).limit(4);
stream1.forEach(t-> System.out.println());
Stream<Double> stream2 = Stream.generate(Math::random).limit(4);
stream2.forEach(System.out::println);
}
二、中间操作
在这里所用的EmployeeData 数据
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;
}
}
filter
List<Employee> list = EmployeeData.getEmployees();
Stream<Employee> stream = list.stream();
stream.filter(e -> e.getSalary() > 8000).forEach(System.out::println);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
limit
Stream<Employee> stream1 = list.stream();
stream1.limit(3).forEach(System.out::println);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
limit
Stream<Employee> stream2 = list.stream();
stream2.skip(3).forEach(System.out::println);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
distinct
List<Integer> list1 = Arrays.asList(1, 2, 2, 2, 5, 6, 7);
list1.stream().distinct().forEach(System.out::println);
排序
@Test
public void test2() {
List<Integer> list = Arrays.asList(1, 2, 3, 5, 6, -9, 0, 8, -99);
String[] strArr = { "g","f","bc", "dee", "f" };
list.stream().sorted().forEach(System.out::println);
System.out.println("-----------");
Arrays.stream(strArr).sorted().forEach(System.out::println);
System.out.println("-----------");
List<Employee> employees = EmployeeData.getEmployees();
employees.stream().sorted((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())).forEach(System.out::println);
System.out.println("-----------");
employees.stream().sorted((e1, e2) -> Integer.compare(e1.getAge(), e2.getAge())).forEach(System.out::println);
}
三、终止操作
forEach
List<Integer> list1 = Arrays.asList(1, 2, 3, 4);
list1.forEach(System.out::println);
匹配与查找
@Test
public void test1() {
List<Employee> employees = EmployeeData.getEmployees();
boolean allMatch = employees.stream().allMatch(e -> e.getAge() > 18);
boolean anyMatch = employees.stream().anyMatch(e -> e.getSalary() > 1000);
boolean noneMatch = employees.stream().noneMatch(e -> e.getName().startsWith("马"));
Optional<Employee> employee = employees.stream().findFirst();
System.out.println(employee);
Optional<Employee> any = employees.stream().findAny();
System.out.println(any);
long count = employees.stream().filter(e -> e.getSalary() > 5000).count();
System.out.println(count);
Stream<Double> salaryStream = employees.stream().map(e -> e.getSalary());
Optional<Double> max = salaryStream.max(Double::compare);
System.out.println(max);
Optional<Employee> employee1 = employees.stream().min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
System.out.println(employee1);
employees.stream().forEach(System.out::println);
}
归约
@Test
public void test2() {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Integer sum = list.stream().reduce(0, Integer::sum);
System.out.println(sum);
List<Employee> employees = EmployeeData.getEmployees();
Stream<Double> salaryStraem = employees.stream().map(Employee::getSalary);
Optional<Double> sum1 = salaryStraem.reduce((s1, s2) -> s1 + s2);
System.out.println(sum1);
}
收集
@Test
public void test3() {
List<Employee> employees = EmployeeData.getEmployees();
List<Employee> collect = employees.stream().filter(employee -> employee.getSalary() > 8000).collect(Collectors.toList());
collect.forEach(System.out::println);
Set<Employee> collect1 = employees.stream().filter(employee -> employee.getSalary() > 8000).collect(Collectors.toSet());
collect1.forEach(System.out::println);
}
|