stream().map(Person::getName).collect(Collectors.toList()); // Accumulate names into a TreeSet Set<String> set = people.stream().map(Person::getName) .collect(Collectors.toCollection(TreeSet::new)); // Convert
System.out.println(Stream.of("a", "b", "c","a").collect(Collectors.toSet())); 2.Collectors.toCollection(TreeSet::new):转换成特定的set集合。 TreeSet<String> treeSet = Stream.of("a", "c", "b", "a").collect(Collectors.toCollection(TreeSet::new)); System.out.println(treeSet);...
// 1. import static java.util.stream.Collectors.collectingAndThen; // 2. import static java.util.stream.Collectors.toCollection; studentList = studentList.stream().collect( collectingAndThen( toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getName))), ArrayList::new) ); out....
people.stream().collect(Collectors.minBy(Comparator.comparing(Person::getAge)));// Optional[Person(id=1004, name=赵六, birthday=2002-06-08, age=21, weight=62.34)], 注意返回类型是Optionalpeople.stream().collect(Collectors.maxBy(Comparator.comparing(Person::getAge)));// Optional[Person(id=10...
比如可以使用 toMap 方法从学生列表中提取出名字、年龄的 HashMap,可以使用 toCollection 方法从学生列表中提取出按 TreeSet 类型存储的学生集合(按年龄排序生成树)。 public void convertToCollection(){ Map<String, Long> map = students.stream() .collect(Collectors.toMap(Student::getName, Student::getAge)...
Set<Integer> set = new HashSet<>(Arrays.asList(44444, 2333, 12432, 53232)); System.out.println("---"); List<Integer> resList = set.stream() .collect(Collectors.toList()); System.out.println(resList); System.out.println("---"); List<Integer> list2 = set.stream()...
List<Human> result = list.stream().limit(3).collect(toList());1. 映射map 对流中的每个元素执行一个函数,使得元素转换成另一种类型输出。流会将每一个元素输送给map函数,并执行map中的Lambda表达式,最后将执行结果存入一个新的流中。 如,获取每个人的姓名(实则是将 Human 类型转换成String类型): ...
TreeSet<String>treeSetResult = list.stream().collect(Collectors.toCollection(TreeSet::new)); treeSetResult.forEach(System.out::println); 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 输出结果为: 四、对Stream的字符串拼接 ...
list.stream().sorted(Comparator.comparing(String::length).reversed() ).limit(3).collect(Collectors.toList()); System.out.println(resultList); 1. 2. 3. 4. 5. 6. 7. allMatch函数 检查是否匹配所有元素,只有全部符合才返回true List<String> list = Arrays.asList("springboot", "springcloud"...
在Java中,可以使用stream方法对List进行操作,并使用collect方法对stream中的元素进行收集。常见的收集操作包括:1. Collectors.toList():将stream中...