List<Student> studentList = Lists.newArrayList(new Student("路飞", 22, 175), new Student("红发", 40, 180),
new Student("白胡子", 50, 185), new Student("白胡子", 60, 185));
Optional<Student> maxStudent = studentList.stream().max(Comparator.comparingInt(Student::getHeight));
Optional<Student> minStudent = studentList.stream().min(Comparator.comparingInt(Student::getHeight));
Double avgAge = studentList.stream().collect(Collectors.averagingInt(Student::getAge));
IntSummaryStatistics summaryStatistics = studentList.stream().collect(Collectors.summarizingInt(Student::getAge));
long count = summaryStatistics.getCount();
int max = summaryStatistics.getMax();
int min = summaryStatistics.getMin();
long sum = summaryStatistics.getSum();
String join1 = studentList.stream().map(Student::getName).collect(Collectors.joining());
String join2 = studentList.stream().map(Student::getName).collect(Collectors.joining(","));
List<String> nameList = studentList.stream().map(Student::getName).collect(Collectors.toList());
Set<String> nameSet = studentList.stream().map(Student::getName).collect(Collectors.toSet());
List<Student> uniqueStudentList = studentList.stream().distinct().collect(Collectors.toList());
List<Student> attrUniqueStudentList = studentList.stream().collect(collectingAndThen(toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getName))), ArrayList::new));
Map<String, Integer> studentMap = studentList.stream().collect(Collectors.toMap(Student::getName, Student::getAge));
public class Student {
private String name;
private Integer age;
private Integer height;
public Student(String name, Integer age, Integer height) {
this.name = name;
this.age = age;
this.height = height;
}
public Student() {
}
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理