演示点一:
筛选年龄小于21
,且薪水大于2000
的数据。
第一种写法:
List<User> userList = Arrays.asList(
new User("小明", 20, 3000),
new User("小红", 20, 2000),
new User("小张", 21, 3000)
);
Predicate<User> predicate1 = user -> user.getAge() < 21;
Predicate<User> predicate2 = user -> user.getSalary() > 2000;
List<User> collect = userList.stream()
.filter(predicate1)
.filter(predicate2)
.collect(Collectors.toList());
System.out.println("collect = " + collect);
第二种写法:
List<User> userList = Arrays.asList(
new User("小明", 20, 3000),
new User("小红", 20, 2000),
new User("小张", 21, 3000)
);
List<User> collect = userList.stream()
.filter(user -> user.getAge() < 21)
.filter(user -> user.getSalary() > 2000)
.collect(Collectors.toList());
System.out.println("collect = " + collect);
第三种写法:
List<User> userList = Arrays.asList(
new User("小明", 20, 3000),
new User("小红", 20, 2000),
new User("小张", 21, 3000)
);
Predicate<User> predicate1 = user -> user.getAge() < 21;
Predicate<User> predicate2 = user -> user.getSalary() > 2000;
List<User> collect = userList.stream()
.filter(predicate1.and(predicate2))
.collect(Collectors.toList());
System.out.println("collect = " + collect);
collect = [User(name=小明, age=20, salary=3000)]
演示点二:
筛选年龄小于21
,或薪水大于2000
的数据。
List<User> userList = Arrays.asList(
new User("小明", 20, 3000),
new User("小红", 20, 2000),
new User("小张", 21, 3000)
);
Predicate<User> predicate1 = user -> user.getAge() < 21;
Predicate<User> predicate2 = user -> user.getSalary() > 2000;
List<User> collect = userList.stream()
.filter(predicate1.or(predicate2))
.collect(Collectors.toList());
System.out.println("collect = " + collect);
collect = [User(name=小明, age=20, salary=3000), User(name=小红, age=20, salary=2000), User(name=小张, age=21, salary=3000)]
演示点三:
filter的使用:筛选字符串长度大于等于5
的字符串。
List<String> list = Arrays.asList("Hello", "World", "Java");
list.stream()
.filter(str -> str.length() >= 5)
.forEach(str -> System.out.println(str));
Hello
World
演示点四
limit 截取流中的元素
List<String> list = Arrays.asList("Hello", "World", "Java");
list.stream()
.limit(2)
.forEach(str -> System.out.println(str));
Hello
World
演示点五
sorted 排序,默认进行升序排列
List<Integer> list = Arrays.asList(1,6,9,3,8,12,0,29);
list.stream()
.sorted()
.forEach(num -> System.out.println(num));
0
1
3
6
8
9
12
29
降序排列:
List<Integer> list = Arrays.asList(1,6,9,3,8,12,0,29);
list.stream()
.sorted(Comparator.reverseOrder())
.forEach(num -> System.out.println(num));
29
12
9
8
6
3
1
0
演示点六
max 求最大值,min 最小值, count 长度
List<Integer> list = Arrays.asList(1, 6, 9, 3, 8, 12, 0, 29);
System.out.println(list.stream()
.max(Integer::compareTo)
.get());
System.out.println(list.stream()
.min(Integer::compareTo)
.get());
System.out.println(list.stream()
.count());
29
0
8
演示点七
stream的map操作:对流操作,返回一个新的流。
List<Integer> list = Arrays.asList(1, 6, 9, 3, 8, 12, 0, 29);
list.stream()
.map(num -> num + 10)
.forEach(num -> System.out.println(num));
演示点八
reduce 根据指定的计算模型将Stream中的值计算得到一个最终结果。
List<Integer> list = Arrays.asList(1, 6, 9, 3, 8, 12, 0, 29);
System.out.println(list.stream()
.reduce((sum, num) -> sum + num).get());
68