发布于 更新于
AI总结: 过滤
List result = list.stream()
.filter(str -> str.length() > 5)
...
List<String> result = list.stream()
.filter(str -> str.length() > 5)
.collect(Collectors.toList());
List<String> result = list.stream()
.map(str -> str.toUpperCase())
.collect(Collectors.toList());
List<Integer> result = list.stream()
.map(c -> c.getId())
.collect(Collectors.toList());
List<Long> longList = stringList.stream()
.map(Long::parseLong)
.collect(Collectors.toList());
List<String> result = list.stream()
.sorted((str1, str2) -> str1.length() - str2.length())
.collect(Collectors.toList());
List<Person> result = list.stream()
.sorted(Comparator.comparing(Person::getAge).reversed())
.collect(Collectors.toList());
// 按年龄分组
Map<Integer, List<Student>> ageMap = studentList.stream()
.collect(Collectors.groupingBy(Student::getAge));
// key重复时, 取前值
Map<Long, User> map = list.stream()
.collect(Collectors.toMap(User::getId, Function.identity(), (v1, v2) -> v1))
// key重复时, 取后值
Map<Long, User> map = list.stream()
.collect(Collectors.toMap(User::getId, Function.identity(), (v1, v2) -> v2))
List<T> list = Arrays.stream(arrays).boxed().collect(Collectors.toList());