Java

发布于 更新于

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 转 List

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));

转map

// 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))

array转list

List<T> list = Arrays.stream(arrays).boxed().collect(Collectors.toList());