自用的笔记, 有 需要多看
自定义实体
@Data
class Student{
private String name;
private Integer age;
private Double height;
public Student() {
}
}
假数据
Student s1 = new Student();
s1.setAge(20);
s1.setName("cookie");
s1.setHeight(180d);
Student s2 = new Student();
s2.setAge(30);
s2.setName("cookie");
s2.setHeight(180d);
Student s3 = new Student();
s3.setAge(40);
s3.setName("bob");
s3.setHeight(175d);
Student s4 = new Student();
s4.setAge(40);
s4.setName("bob");
s4.setHeight(180d);
// 存入list集合
List<Student> list = new ArrayList<>();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
/**
* 需求一(一层分组):根据Age分组
*/
System.out.println("需求一(一层分组):根据Age分组");
Map<Integer, List<Student>> collect = list.stream().collect(Collectors.groupingBy(Student::getAge));
for (Integer age : collect.keySet()) {
System.out.println("key:" + age + "\tvalue:" + collect.get(age));
}
/**
* 控制台结果:
* key:20 value:[Student(name=cookie, age=20, height=180.0)]
* key:40 value:[Student(name=bob, age=40, height=175.0), Student(name=bob, age=40, height=180.0)]
* key:30 value:[Student(name=cookie, age=30, height=180.0)]
*/
/**
* 需求二: 先根据name分组,然后再根据身高分组
*/
System.out.println("需求二: 先根据name分组,然后再根据身高分组");
Map<String, Map<Double, List<Student>>> collect1 = list.stream()
.collect(Collectors.groupingBy(Student::getName, Collectors.groupingBy(Student::getHeight)));
Set<String> namesGroup = collect1.keySet();
for (String namekey : namesGroup) {
Map<Double, List<Student>> heightGroupMap = collect1.get(namekey);
Set<Double> height = heightGroupMap.keySet();
for (Double h : height) {
System.out.println("name:" + namekey + " height:" + heightGroupMap.get(h));
}
}
/**
* 控制台结果:
* name:bob height:[Student(name=bob, age=40, height=175.0)]
* name:bob height:[Student(name=bob, age=40, height=180.0)]
* name:cookie height:[Student(name=cookie, age=20, height=180.0), Student(name=cookie, age=30, height=180.0)]
*/
/**
* 需求三: 自定义key返回 形式如下: age_height bob_175
*/
System.out.println("需求三: 自定义key返回 形式如下: age_height bob_175");
Map<String, List<Student>> collect2 = list.stream()
.collect(Collectors.groupingBy(c -> c.getName() + "_" + c.getHeight()));
for (String customKey : collect2.keySet()) {
System.out.println("key:" + customKey +" value:"+ collect2.get(customKey));
}
/**
* 控制台结果:
* key:bob_180.0 value:[Student(name=bob, age=40, height=180.0)]
* key:bob_175.0 value:[Student(name=bob, age=40, height=175.0)]
* key:cookie_180.0 value:[Student(name=cookie, age=20, height=180.0), Student(name=cookie, age=30, height=180.0)]
*/
/**
* 需求: 根据身高排序,如果身高相同,根据年龄排序,如果年龄依然相同,根据名称字母顺序排序
*/
List<Student> collect3 = list.stream().sorted(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
// 这里前面的减去后面的是升序, 反之这是降序
if (!o1.getHeight().equals(o2.getHeight())) {
return (int) (o1.getHeight() - o2.getHeight());
}
if (!o1.getAge().equals(o2.getAge())) {
return o1.getAge() - o2.getAge();
}
return o1.getName().compareTo(o2.getName());
}
}).collect(Collectors.toList());
System.out.println(collect3);
/**
* 控制台结果:
* [Student(name=bob, age=40, height=175.0),
* Student(name=cookie, age=20, height=180.0),
* Student(name=cookie, age=30, height=180.0),
* Student(name=bob, age=40, height=180.0)]
*/
// 注: 当然上面的也可以做一个简化
List<Student> collect3 = list.stream().sorted((o1, o2) -> {
// 这里前面的减去后面的是升序, 反之这是降序
if (!o1.getHeight().equals(o2.getHeight())) {
return (int) (o1.getHeight() - o2.getHeight());
}
if (!o1.getAge().equals(o2.getAge())) {
return o1.getAge() - o2.getAge();
}
return o1.getName().compareTo(o2.getName());
}).collect(Collectors.toList());
List<Student> collect4 = list.stream()
.sorted(Comparator.comparingDouble(Student::getHeight)
.thenComparingInt(Student::getAge)
.thenComparing(Student::getName))
.collect(Collectors.toList());
System.out.println(collect4);
/**
* 控制台结果:
* [Student(name=bob, age=40, height=175.0),
* Student(name=cookie, age=20, height=180.0),
* Student(name=cookie, age=30, height=180.0),
* Student(name=bob, age=40, height=180.0)]
*/
// 注意:
// 方式一,升序降序是通过返回的正负,
// 方式二而是通过方法, 现在我们首先通过身高降序, 我们只需要在条件的后面加一个reversed()后缀方法即可
List<Student> collect4 = list.stream().sorted(Comparator.comparingDouble(Student::getHeight).reversed()
.thenComparingInt(Student::getAge)
.thenComparing(Student::getName)
).collect(Collectors.toList());
System.out.println(collect4);
/**
* 修改之后控制台结果:
* [Student(name=cookie, age=20, height=180.0),
* Student(name=cookie, age=30, height=180.0),
* Student(name=bob, age=40, height=180.0),
* Student(name=bob, age=40, height=175.0)]
*/
/**
* 需求: 统计年龄之和
*/
int ageSum = list.stream().mapToInt(Student::getAge).sum();
/**
* 求年龄平均值
*/
Double ageAvg1 = list.stream().collect(Collectors.averagingInt(Student::getAge));
// 或者
double ageAvg2 = list.stream().mapToInt(Student::getAge).average().getAsDouble();
/**
* 求年龄最大值
*/
int maxAge = list.stream().mapToInt(Student::getAge).max().getAsInt();
/**
* 最小值
*/
int minAge = list.stream().mapToInt(Student::getAge).min().getAsInt();
缓慢总结中~~~~