简单分组
- 代码示例
import java.util.*;
import java.util.stream.Collectors;
public class SimpleGrouping {
public static void main(String[] args) {
List<Person> people = Arrays.asList(
new Person("Alice", 30),
new Person("Bob", 25),
new Person("Charlie", 30),
new Person("David", 25),
new Person("Eve", 35)
);
Map<Integer, List<Person>> groupedByAge = people.stream()
.collect(Collectors.groupingBy(Person::getAge));
groupedByAge.forEach((age, personList) -> {
System.out.println("Age: " + age);
personList.forEach(person -> System.out.println(person));
});
}
}
- 输出结果
Age: 25
Person{name='Bob', age=25}
Person{name='David', age=25}
Age: 30
Person{name='Alice', age=30}
Person{name='Charlie', age=30}
Age: 35
Person{name='Eve', age=35}
分组并统计
- 代码示例
import java.util.*;
import java.util.stream.Collectors;
public class GroupingAndCounting {
public static void main(String[] args) {
List<Person> people = Arrays.asList(
new Person("Alice", 30),
new Person("Bob", 25),
new Person("Charlie", 30),
new Person("David", 25),
new Person("Eve", 35)
);
Map<Integer, Long> countByAge = people.stream()
.collect(Collectors.groupingBy(
Person::getAge,
Collectors.counting()
));
countByAge.forEach((age, count) -> {
System.out.println("Age: " + age + ", Count: " + count);
});
}
}
- 数据结果
Age: 25, Count: 2
Age: 30, Count: 2
Age: 35, Count: 1
分组并进行数据转换
- 按年龄分组,并将每组中的 Person 对象转换为姓名列表:
- 代码示例
import java.util.*;
import java.util.stream.Collectors;
public class GroupingAndMapping {
public static void main(String[] args) {
List<Person> people = Arrays.asList(
new Person("Alice", 30),
new Person("Bob", 25),
new Person("Charlie", 30),
new Person("David", 25),
new Person("Eve", 35)
);
Map<Integer, List<String>> namesByAge = people.stream()
.collect(Collectors.groupingBy(
Person::getAge,
Collectors.mapping(Person::getName, Collectors.toList())
));
namesByAge.forEach((age, names) -> {
System.out.println("Age: " + age + ", Names: " + names);
});
}
}
- 输出结果
Age: 25, Names: [Bob, David]
Age: 30, Names: [Alice, Charlie]
Age: 35, Names: [Eve]
多级分组
- 示例代码
import java.util.*;
import java.util.stream.Collectors;
public class MultiLevelGrouping {
public static void main(String[] args) {
List<Person> people = Arrays.asList(
new Person("Alice", 30),
new Person("Bob", 25),
new Person("Charlie", 30),
new Person("David", 25),
new Person("Eve", 35)
);
Map<Integer, Map<Character, List<Person>>> multiLevelGrouping = people.stream()
.collect(Collectors.groupingBy(
Person::getAge,
Collectors.groupingBy(person -> person.getName().charAt(0))
));
multiLevelGrouping.forEach((age, group) -> {
System.out.println("Age: " + age);
group.forEach((initial, persons) -> {
System.out.println(" Initial: " + initial);
persons.forEach(person -> System.out.println(" " + person));
});
});
}
}
- 输出结果
Age: 25
Initial: B
Person{name='Bob', age=25}
Initial: D
Person{name='David', age=25}
Age: 30
Initial: A
Person{name='Alice', age=30}
Initial: C
Person{name='Charlie', age=30}
Age: 35
Initial: E
Person{name='Eve', age=35}
分组并收集到不同的集合
- 按年龄分组,并将每组中的 Person 对象收集到 Set 中:
- 代码示例
import java.util.*;
import java.util.stream.Collectors;
public class GroupingToSet {
public static void main(String[] args) {
List<Person> people = Arrays.asList(
new Person("Alice", 30),
new Person("Bob", 25),
new Person("Charlie", 30),
new Person("David", 25),
new Person("Eve", 35)
);
Map<Integer, Set<Person>> groupedByAgeToSet = people.stream()
.collect(Collectors.groupingBy(
Person::getAge,
Collectors.toSet()
));
groupedByAgeToSet.forEach((age, personSet) -> {
System.out.println("Age: " + age);
personSet.forEach(person -> System.out.println(person));
});
}
}
- 输出结果
Age: 25
Person{name='Bob', age=25}
Person{name='David', age=25}
Age: 30
Person{name='Alice', age=30}
Person{name='Charlie', age=30}
Age: 35
Person{name='Eve', age=35}
自定义收集器
- 代码示例
import java.util.*;
import java.util.stream.Collectors;
public class GroupingWithCustomCollector {
public static void main(String[] args) {
List<Person> people = Arrays.asList(
new Person("Alice", 30),
new Person("Bob", 25),
new Person("Charlie", 30),
new Person("David", 25),
new Person("Eve", 35)
);
Map<Integer, Double> averageAgeByGroup = people.stream()
.collect(Collectors.groupingBy(
Person::getAge,
Collectors.averagingInt(Person::getAge)
));
averageAgeByGroup.forEach((age, avgAge) -> {
System.out.println("Age: " + age + ", Average Age: " + avgAge);
});
}
}
- 输出结果
Age: 25, Average Age: 25.0
Age: 30, Average Age: 30.0
Age: 35, Average Age: 35.0