直接贴代码:
package com.c8a.stream;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Test;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class StreamAPI {
@Data
@AllArgsConstructor
@NoArgsConstructor
static class Employee {
private Integer id;
private String name;
private Integer age;
private Double salary;
}
//生成数据
public static List empList() {
List employeeList = new ArrayList();
employeeList.add(new Employee(1001, "逍遥子", 32, 1000.00));
employeeList.add(new Employee(1002, "杰克马", 34, 3453.00));
employeeList.add(new Employee(1003, "张三", 32, 5675.34));
employeeList.add(new Employee(1004, "李四", 67, 12300.00));
employeeList.add(new Employee(1005, "王五", 52, 104230.00));
employeeList.add(new Employee(1006, "赵六", 45, 342300.00));
return employeeList;
}
//通过集合
@Test
public void Test1() {
List employeeList = empList();
//顺序流
Stream stream = employeeList.stream();
stream.forEach(System.out::println);
//并行流
Stream employeeStream = employeeList.parallelStream();
employeeStream.forEach(System.out::println);
}
//通过数组
@Test
public void Test2() {
int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7};
IntStream stream = Arrays.stream(arr);
}
//通过Stream的of
@Test
public void Test3() {
Stream integerStream = Stream.of(1, 2, 3, 4, 5, 6);
}
//创建无限流
@Test
public void Test4() {
// Stream.iterate(0, t -> t + 2).limit(10).forEach(System.out::println);
Stream.generate(Math::random).limit(10).forEach(System.out::println);
}
//筛选与切片
@Test
public void Test5() {
//filter
List employeeList = empList();
// employeeList.stream().filter(employee -> employee.getSalary()>1250.00).forEach(System.out::println);
//limit
// employeeList.stream().filter(employee -> employee.getSalary()>1250.00).limit(2).forEach(System.out::println);
//skip
// employeeList.stream().filter(employee -> employee.getSalary()>1250.00).skip(2).forEach(System.out::println);
//distinct
employeeList.add(new Employee(105, "张三5", 35, 1500.0));
employeeList.stream().filter(employee -> employee.getSalary() > 1250.00).distinct().forEach(System.out::println);
System.out.println(employeeList);
}
//映射
@Test
public void Test6() {
//map(Function f)
System.out.println("***************************************map(Function f)");
List stringList = Arrays.asList("aa", "bb", "cc", "dd", "ee", "ff");
stringList.stream().map(s -> s.toUpperCase()).forEach(System.out::println);
//练习1
System.out.println("***************************************练习1");
empList().stream().map(Employee::getName).filter(name -> name.length() > 2).forEach(System.out::println);
//练习2
System.out.println("***************************************练习2");
Stream> streamStream = stringList.stream().map(StreamAPI::fromStringToStream);
streamStream.forEach(s -> {
s.forEach(System.out::println);
});
//flatMap(Function f) 练习2简化版
System.out.println("***************************************练习2简化版");
stringList.stream().flatMap(StreamAPI::fromStringToStream).forEach(System.out::println);
//mapToDouble(ToDoubleFunction f)
System.out.println("***************************************mapToDouble");
List integerList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
integerList.stream().mapToDouble(i -> i * 2).forEach(System.out::println);
//mapToInt( ToIntFunction f)
System.out.println("***************************************mapToInt");
List doubleList = Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0);
doubleList.stream().mapToDouble(d -> d + 1).forEach(System.out::println);
//mapToLong(ToLongFunction f)
System.out.println("***************************************mapToLong");
List intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
intList.stream().mapToLong(l -> l + 100).forEach(System.out::println);
}
//将字符事中的多个字符构成的集合转换为对应的Stream的实例
public static Stream fromStringToStream(String str) {
ArrayList list = new ArrayList<>();
for (Character c : str.toCharArray()) {
list.add(c);
}
return list.stream();
}
//排序
@Test
public void Test7() {
//sorted()
System.out.println("******sorted()******");
List integerList = Arrays.asList(324, 4545, 2342, 56, 23, 454, 6761, 45634, 123, 657, 6789);
integerList.stream().sorted().forEach(System.out::println);
//sorted(Comparator com)
System.out.println("******sorted(Comparator com)******");
empList().stream().sorted((e1, e2) -> {
//先比较年龄
int compare = Integer.compare(e1.getAge(), e2.getAge());
if (compare != 0) {
return compare;
} else {
//如果相同再比较薪水
return Double.compare(e1.getSalary(), e2.getSalary());
}
}).forEach(System.out::println);
}
//终止操作
@Test
public void Test8() {
List employeeList = empList();
//allMatch(Predicate p) 检查是否匹配所有元素。
System.out.println("******allMatch(Predicate p)******");
boolean b = employeeList.stream().allMatch(employee -> employee.getAge() > 33);
System.out.println(b);
//anyMatch(Predicate p) 检查是否至少匹配一个元素。
System.out.println("******anyMatch(Predicate p)******");
boolean bb = employeeList.stream().anyMatch(employee -> employee.getAge() > 33);
System.out.println(bb);
//noneMatch(Predicate p) 检查是否没有匹配的元素。
System.out.println("******noneMatch(Predicate p)******");
boolean bbb = employeeList.stream().anyMatch(employee -> employee.getAge() > 33);
System.out.println(bbb);
//findFirst() 返回第一个元素
System.out.println("******findFirst()******");
Optional first = employeeList.stream().findFirst();
System.out.println(first);
//findAny() 返回当前流中的任意元素
System.out.println("******findAny()******");
Optional any = employeeList.parallelStream().findAny();
System.out.println(any);
//count 返回流中元素的总个数
System.out.println("******count()******");
long count = employeeList.parallelStream().count();
System.out.println(count);
//max(Comparator c) 返回流中最大值
System.out.println("******max(Comparator c)******");
Optional max = employeeList.stream().map(Employee::getSalary).max(Double::compare);
System.out.println(max);
//min(Comparator c) 返回流中最小值
System.out.println("******min(Comparator c)******");
Optional min = employeeList.stream().min(Comparator.comparingDouble(Employee::getSalary));
System.out.println(min);
//forEach(Consumer c) 返回流中最小值
System.out.println("******forEach(Consumer c)******");
employeeList.forEach(System.out::println);
}
//规约
@Test
public void Test9() {
//reduce(T iden,BinaryOperator b)
//练习1:计拿1-10的自然数的和
System.out.println("******计拿1-10的自然数的和******");
List integerList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Integer reduce = integerList.stream().reduce(0, Integer::sum);
System.out.println(reduce);
//计算公司工资总和
System.out.println("******计算公司工资总和******");
Optional reduce2 = empList().stream().map(Employee::getSalary).reduce(Double::sum);
System.out.println(reduce2);
//reduce(BinaryOperator b)
}
//收集
@Test
public void Test10() {
//collect(Collector c)
//toList()
System.out.println("******toList******");
empList().stream().filter(e -> e.getSalary() > 6000.00).collect(Collectors.toList()).forEach(System.out::println);
//toSet()
System.out.println("******toSet******");
empList().stream().filter(e -> e.getSalary() > 6000.00).collect(Collectors.toSet()).forEach(System.out::println);
}
}



