目录
1、filter:过滤出年龄大于18岁的人
2、将字符串列表中的每个元素转换为大写字母
3、flatMap:将多个字符串列表合并为一个字符串列表
4、sorted:按照年龄从小到大排序
5、distinct:去除重复的元素
6、limit:限制输出的元素数量
7、skip:跳过前面的元素
8、reduce:将字符串列表中的元素合并为一个字符串
9、forEach:对流中的每个元素进行操作
10、peek:对流中的每个元素进行操作,返回一个新的流
11、anyMatch:判断字符串列表中是否有以"a"开头的元素
12、allMatch:判断字符串列表中是否所有元素都以"a"开头
13、noneMatch:判断字符串列表中是否没有以"x"开头的元素
14、findFirst:返回字符串列表中的第一个元素
15、findAny:返回字符串列表中的任意一个元素
16、count:返回字符串列表中的元素数量
17、max:返回字符串列表中的最长字符串
18、min:返回字符串列表中的最短字符串
19、toArray:将流转换为数组
20、groupBy:根据某个属性对流中的元素进行分组,返回一个 Map
21、partitionBy:根据某个条件对流中的元素进行分组,返回一个 Map
22、toSet:将流中的元素收集到一个 Set 中
23、toMap:将流中的元素收集到一个 Map 中
24、toArray:将流中的元素收集到一个数组中
25、maxBy:返回流中的最大值,可以指定比较器
26、minBy:返回流中的最小值,可以指定比较器
27、concat:将两个流合并为一个流
List persons = new ArrayList<>();
persons.add(new Person("张三", 20));
persons.add(new Person("李四", 16));
persons.add(new Person("王五", 25));
List adults = persons.stream()
.filter(person -> person.getAge() > 18)
.collect(Collectors.toList());
List words = Arrays.asList("apple", "banana", "orange");
List upperCaseWords = words.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
List> lists = Arrays.asList(
Arrays.asList("apple", "banana"),
Arrays.asList("orange", "pear"),
Arrays.asList("grape", "pineapple")
);
List words = lists.stream()
.flatMap(Collection::stream)
.collect(Collectors.toList());
List persons = new ArrayList<>();
persons.add(new Person("张三", 20));
persons.add(new Person("李四", 16));
persons.add(new Person("王五", 25));
List sortedPersons = persons.stream()
.sorted(Comparator.comparing(Person::getAge))
.collect(Collectors.toList());
List words = Arrays.asList("apple", "banana", "orange", "banana", "apple");
List distinctWords = words.stream()
.distinct()
.collect(Collectors.toList());
List words = Arrays.asList("apple", "banana", "orange", "pear", "grape");
List limitedWords = words.stream()
.limit(3)
.collect(Collectors.toList());
List words = Arrays.asList("apple", "banana", "orange", "pear", "grape");
List skippedWords = words.stream()
.skip(2)
.collect(Collectors.toList());
List words = Arrays.asList("apple", "banana", "orange", "pear", "grape");
String result = words.stream()
.reduce("", (a, b) -> a + b);
List persons = new ArrayList<>();
persons.add(new Person("张三", 20));
persons.add(new Person("李四", 16));
persons.add(new Person("王五", 25));
persons.stream().forEach(item -> {
item.setAge(item.getAge()-1);
System.out.println(item);
});
List persons = new ArrayList<>();
persons.add(new Person("张三", 20));
persons.add(new Person("李四", 16));
persons.add(new Person("王五", 25));
persons.stream().peek(item -> {
item.setAge(item.getAge()-1);
}).collect(Collectors.toList());
System.out.println(persons);
List words = Arrays.asList("apple", "banana", "orange", "pear", "grape");
boolean result = words.stream()
.anyMatch(word -> word.startsWith("a"));
List words = Arrays.asList("apple", "apricot", "avocado");
boolean result = words.stream()
.allMatch(word -> word.startsWith("a"));
List words = Arrays.asList("apple", "banana", "orange", "pear", "grape");
boolean result = words.stream()
.noneMatch(word -> word.startsWith("x"));
List words = Arrays.asList("apple", "banana", "orange", "pear", "grape");
Optional result = words.stream()
.findFirst();
List words = Arrays.asList("apple", "banana", "orange", "pear", "grape");
Optional result = words.stream()
.findAny();
List words = Arrays.asList("apple", "banana", "orange", "pear", "grape");
long count = words.stream()
.count();
List words = Arrays.asList("apple", "banana", "orange", "pear", "grape");
Optional result = words.stream()
.max(Comparator.comparing(String::length));
List words = Arrays.asList("apple", "banana", "orange", "pear", "grape");
Optional result = words.stream()
.min(Comparator.comparing(String::length));
List words = Arrays.asList("apple", "banana", "orange", "pear", "grape");
String[] array = words.stream()
.toArray(String[]::new);
List persons = new ArrayList<>();
persons.add(new Person("张三", 20));
persons.add(new Person("李四", 16));
persons.add(new Person("王五", 25));
Map> groups = persons.stream()
.collect(Collectors.groupingBy(Person::getAge));
List persons = new ArrayList<>();
persons.add(new Person("张三", 20));
persons.add(new Person("李四", 16));
persons.add(new Person("王五", 25));
Map> groups = persons.stream()
.collect(Collectors.partitioningBy(person -> person.getAge() > 18));
List words = Arrays.asList("apple", "banana", "orange", "pear", "grape");
Set set = words.stream()
.collect(Collectors.toSet());
List persons = new ArrayList<>();
persons.add(new Person("张三", 20));
persons.add(new Person("李四", 16));
persons.add(new Person("王五", 25));
Map map = persons.stream()
.collect(Collectors.toMap(Person::getName, Person::getAge));
List words = Arrays.asList("apple", "banana", "orange", "pear", "grape");
String[] array = words.stream()
.toArray(String[]::new);
List numbers = Arrays.asList(1, 2, 3, 4, 5);
Optional result = numbers.stream()
.max(Integer::compareTo);
List numbers = Arrays.asList(1, 2, 3, 4, 5);
Optional result = numbers.stream()
.min(Integer::compareTo);
List words1 = Arrays.asList("apple", "banana", "orange");
List words2 = Arrays.asList("pear", "grape");
List words = Stream.concat(words1.stream(), words2.stream())
.collect(Collectors.toList());