一、StreamAPI概述
1. Stream与集合的区别
- Stream关注的是对数据的计算,与CPU打交道
- 集合关注的是数据的存储,与内存打交道
2.Stream特征
- Stream 自己不会存储元素
- Stream 不会改变源数据。相反,会返回一个持有结果的新Stream
- Stream 操作是延迟执行的。这意味着会等到需要结果的时候才执行
3.Stream执行流程
- Stream的实例化
- 一系列中间操作(过滤、映射、…)
- 终止操作
4.补充说明
- 一个中间操作链,对数据源的数据进行处理
- 一旦执行终止操作,就执行中间操作链,并产生结果,之后,不会再被使用
二、StreamAPI的使用
1.StreamAPI的实例化方式
public class StreamApiCreateTest {
@Test
public void test1() {
List<User> users = UserData.getUsers();
Stream<User> stream = users.stream();
Stream<User> parallelStream = users.parallelStream();
}
@Test
public void test2() {
int[] arr = new int[]{1, 2, 3, 4, 5, 6};
IntStream stream = Arrays.stream(arr);
User u1 = new User("张三", 18);
User u2 = new User("李四", 27);
User[] arr1 = new User[]{u1, u2};
Stream<User> userStream = Arrays.stream(arr1);
}
@Test
public void test3() {
Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);
}
@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);
}
}
2.StreamAPI的中间操作
public class StreamApiMiddleOptTest {
@Test
public void test1() {
List<User> list = UserData.getUsers();
Stream<User> stream1 = list.stream();
stream1.filter(e -> e.getAge() > 18).forEach(System.out::println);
System.out.println("**********************");
Stream<User> stream2 = list.stream();
stream2.limit(3).forEach(System.out::println);
System.out.println("**********************");
Stream<User> stream3 = list.stream();
stream3.skip(3).forEach(System.out::println);
System.out.println("**********************");
Stream<User> stream4 = list.stream();
stream4.distinct().forEach(System.out::println);
}
@Test
public void test2() {
List<String> list = Arrays.asList("aa", "bb", "cc", "dd");
list.stream().map(str -> str.toUpperCase()).forEach(System.out::println);
System.out.println("*************************");
UserData.getUsers().stream()
.filter(u -> u.getName().length() > 2)
.map(u -> u.getName())
.forEach(System.out::println);
System.out.println("*************************");
}
@Test
public void test3() {
List<Integer> list = Arrays.asList(12, 43, 38, 27, 8, 59, 33);
list.stream().sorted().forEach(System.out::println);
System.out.println("****************************");
List<User> users = UserData.getUsers();
users.stream().sorted((u1, u2) -> Integer.compare(u1.getAge(), u2.getAge()))
.forEach(System.out::println);
}
}
3.StreamAPI的终止操作
public class StreamApiFinalOptTest {
@Test
public void test1() {
List<User> users = UserData.getUsers();
boolean allMatch = users.stream().allMatch(e -> e.getAge() > 18);
System.out.println(allMatch);
System.out.println("************************");
boolean anyMatch = users.stream().anyMatch(e -> e.getAge() > 18);
System.out.println(anyMatch);
System.out.println("************************");
boolean noneMathch = users.stream().noneMatch(e -> e.getName().startsWith("张"));
System.out.println(noneMathch);
System.out.println("************************");
Optional<User> first = users.stream().findFirst();
System.out.println(first);
System.out.println("************************");
Optional<User> any = users.stream().findAny();
System.out.println(any);
}
@Test
public void test2() {
List<User> users = UserData.getUsers();
long count = users.stream().count();
System.out.println(count);
System.out.println("************************");
Optional<Integer> max = users.stream().map(u -> u.getAge()).max(Integer::compare);
System.out.println(max);
System.out.println("************************");
Optional<String> min = users.stream().map(u -> u.getName()).min((s1, s2) -> s1.length() - s2.length());
System.out.println(min);
System.out.println("************************");
users.stream().forEach(System.out::println);
}
@Test
public void test3() {
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);
System.out.println("************************");
List<User> users = UserData.getUsers();
Optional<Integer> all = users.stream().map(User::getAge).reduce(Integer::sum);
System.out.println(all);
}
@Test
public void test4() {
List<User> users = UserData.getUsers();
List<User> collect = users.stream().filter(u -> u.getAge() > 18).collect(Collectors.toList());
collect.stream().forEach(System.out::println);
System.out.println("************************");
Set<User> set = users.stream().filter(u -> u.getAge() > 15).collect(Collectors.toSet());
set.stream().forEach(System.out::println);
}
}