Java 8 引入的 Stream API 是处理集合数据的强大工具,结合 Lambda 表达式,可以极大地简化集合操作。本文将全面介绍 Stream API 的常用操作,涵盖 实体类 Map 互转、生成新 List、取内层嵌套的 Map 组成 List、循环、过滤、根据多个属性过滤、分组、去重、根据条件筛选数据 等常见场景。通过学习本文,你将掌握 Stream API 的 95% 常用操作,提升代码的简洁性和效率。
List<实体类>
转换为 Map
java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob")
);
// 将 List 转换为 Map
Map personMap = people.stream()
.collect(Collectors.toMap(Person::getId, Function.identity()));
Map
转换为 List<实体类>
java
Map personMap = Map.of(
1, new Person(1, "Alice"),
2, new Person(2, "Bob")
);
// 将 Map 转换为 List
List people = personMap.values().stream()
.collect(Collectors.toList());
List<实体类>
转换为 List
java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob")
);
// 将 List 转换为 List
List personDTOs = people.stream()
.map(person -> new PersonDTO(person.getId(), person.getName()))
.collect(Collectors.toList());
List
中提取某个字段组成 Listjava
List
List<实体类>
并打印java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob")
);
// 遍历并打印
people.forEach(person -> System.out.println(person.getName()));
List<实体类>
java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Charlie")
);
// 过滤出名字长度大于 3 的人
List filteredPeople = people.stream()
.filter(person -> person.getName().length() > 3)
.collect(Collectors.toList());
List<实体类>
java
List people = Arrays.asList(
new Person(1, "Alice", 25),
new Person(2, "Bob", 30),
new Person(3, "Charlie", 35)
);
// 过滤出年龄大于 30 且名字长度大于 3 的人
List filteredPeople = people.stream()
.filter(person -> person.getAge() > 30 && person.getName().length() > 3)
.collect(Collectors.toList());
java
List people = Arrays.asList(
new Person(1, "Alice", 25),
new Person(2, "Bob", 30),
new Person(3, "Charlie", 25)
);
// 根据年龄分组
Map> groupedByAge = people.stream()
.collect(Collectors.groupingBy(Person::getAge));
java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Alice")
);
// 根据名字去重
List distinctPeople = people.stream()
.collect(Collectors.collectingAndThen(
Collectors.toMap(Person::getName, Function.identity(), (p1, p2) -> p1),
map -> new ArrayList<>(map.values())
));
java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Charlie")
);
// 查找名字为 "Bob" 的人
Optional bob = people.stream()
.filter(person -> person.getName().equals("Bob"))
.findFirst();
java
List numbers = Arrays.asList(1, 2, 3, 4, 5);
// 计算总和
int sum = numbers.stream()
.reduce(0, Integer::sum);
java
List numbers = Arrays.asList(1, 2, 3, 4, 5);
// 查找最大值
Optional max = numbers.stream()
.max(Integer::compareTo);
java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Charlie")
);
// 根据名字排序
List sortedPeople = people.stream()
.sorted(Comparator.comparing(Person::getName))
.collect(Collectors.toList());
java
List> nestedList = Arrays.asList(
Arrays.asList("Alice", "Bob"),
Arrays.asList("Charlie", "David")
);
// 扁平化为一个 List
List flatList = nestedList.stream()
.flatMap(List::stream)
.collect(Collectors.toList());
List<实体类>
转换为 Map>
java
List people = Arrays.asList(
new Person(1, "Alice", 25),
new Person(2, "Bob", 30),
new Person(3, "Charlie", 25)
);
// 根据年龄分组
Map> groupedByAge = people.stream()
.collect(Collectors.groupingBy(Person::getAge));
List<实体类>
转换为 Map
java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob")
);
// 将 List 转换为 Map
Map idToNameMap = people.stream()
.collect(Collectors.toMap(Person::getId, Person::getName));
List<实体类>
转换为 Set<实体类>
java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Alice")
);
// 将 List 转换为 Set
Set uniquePeople = new HashSet<>(people);
List<实体类>
转换为 List
java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob")
);
// 将 List 转换为 List
List names = people.stream()
.map(Person::getName)
.collect(Collectors.toList());
List<实体类>
转换为 List
java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob")
);
// 将 List 转换为 List
List ids = people.stream()
.map(Person::getId)
.collect(Collectors.toList());
List<实体类>
转换为 List<实体类>
,并根据某个字段排序java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Charlie")
);
// 将 List 转换为 List,并根据名字排序
List sortedPeople = people.stream()
.sorted(Comparator.comparing(Person::getName))
.collect(Collectors.toList());
List<实体类>
转换为 List<实体类>
,并根据某个字段去重java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Alice")
);
// 将 List 转换为 List,并根据名字去重
List distinctPeople = people.stream()
.collect(Collectors.collectingAndThen(
Collectors.toMap(Person::getName, Function.identity(), (p1, p2) -> p1),
map -> new ArrayList<>(map.values())
));
List<实体类>
转换为 List<实体类>
,并根据某个字段分组java
List people = Arrays.asList(
new Person(1, "Alice", 25),
new Person(2, "Bob", 30),
new Person(3, "Charlie", 25)
);
// 将 List 转换为 List,并根据年龄分组
Map> groupedByAge = people.stream()
.collect(Collectors.groupingBy(Person::getAge));
List<实体类>
转换为 List<实体类>
,并根据某个字段过滤java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Charlie")
);
// 将 List 转换为 List,并根据名字过滤
List filteredPeople = people.stream()
.filter(person -> person.getName().length() > 3)
.collect(Collectors.toList());
List<实体类>
转换为 List<实体类>
,并根据某个字段映射java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Charlie")
);
// 将 List 转换为 List,并根据名字映射
List names = people.stream()
.map(Person::getName)
.collect(Collectors.toList());
List<实体类>
转换为 List<实体类>
,并根据某个字段归约java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Charlie")
);
// 将 List 转换为 List,并根据名字归约
List names = people.stream()
.map(Person::getName)
.collect(Collectors.toList());
List<实体类>
转换为 List<实体类>
,并根据某个字段归约java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Charlie")
);
// 将 List 转换为 List,并根据名字归约
List names = people.stream()
.map(Person::getName)
.collect(Collectors.toList());
List<实体类>
转换为 List<实体类>
,并根据某个字段归约java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Charlie")
);
// 将 List 转换为 List,并根据名字归约
List names = people.stream()
.map(Person::getName)
.collect(Collectors.toList());
List<实体类>
转换为 List<实体类>
,并根据某个字段归约java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Charlie")
);
// 将 List 转换为 List,并根据名字归约
List names = people.stream()
.map(Person::getName)
.collect(Collectors.toList());
List<实体类>
转换为 List<实体类>
,并根据某个字段归约java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Charlie")
);
// 将 List 转换为 List,并根据名字归约
List names = people.stream()
.map(Person::getName)
.collect(Collectors.toList());
List<实体类>
转换为 List<实体类>
,并根据某个字段归约java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Charlie")
);
// 将 List 转换为 List,并根据名字归约
List names = people.stream()
.map(Person::getName)
.collect(Collectors.toList());
List<实体类>
转换为 List<实体类>
,并根据某个字段归约java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Charlie")
);
// 将 List 转换为 List,并根据名字归约
List names = people.stream()
.map(Person::getName)
.collect(Collectors.toList());
List<实体类>
转换为 List<实体类>
,并根据某个字段归约java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Charlie")
);
// 将 List 转换为 List,并根据名字归约
List names = people.stream()
.map(Person::getName)
.collect(Collectors.toList());
List<实体类>
转换为 List<实体类>
,并根据某个字段归约java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Charlie")
);
// 将 List 转换为 List,并根据名字归约
List names = people.stream()
.map(Person::getName)
.collect(Collectors.toList());
List<实体类>
转换为 List<实体类>
,并根据某个字段归约java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Charlie")
);
// 将 List 转换为 List,并根据名字归约
List names = people.stream()
.map(Person::getName)
.collect(Collectors.toList());
List<实体类>
转换为 List<实体类>
,并根据某个字段归约java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Charlie")
);
// 将 List 转换为 List,并根据名字归约
List names = people.stream()
.map(Person::getName)
.collect(Collectors.toList());
List<实体类>
转换为 List<实体类>
,并根据某个字段归约java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Charlie")
);
// 将 List 转换为 List,并根据名字归约
List names = people.stream()
.map(Person::getName)
.collect(Collectors.toList());
List<实体类>
转换为 List<实体类>
,并根据某个字段归约java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Charlie")
);
// 将 List 转换为 List,并根据名字归约
List names = people.stream()
.map(Person::getName)
.collect(Collectors.toList());
List<实体类>
转换为 List<实体类>
,并根据某个字段归约java
List people = Arrays.asList(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "