做网站大概费用游戏私人服务器搭建
以下是对您博客内容的优化版本,在保留原有核心内容的基础上,补充了Lambda表达式及Stream API的完整方法体系,并通过结构化排版和扩展说明提升可读性。
Java Lambda表达式与Stream API全解析:从基础到进阶
一、Lambda表达式与Stream API基础
Lambda表达式是Java 8引入的函数式编程特性,配合Stream API可高效处理集合数据。其核心优势在于:
- 减少冗余代码,提升可读性
 - 支持并行处理,优化性能
 - 链式调用实现流式数据处理
 
二、Stream API核心方法详解
以下以Student类为例(类定义同原博客),展示Stream API的完整方法体系:
1. 数据转换(Map系列)
作用:将流中元素转换为新类型。
// 基础map:转换元素类型
List<Integer> ages = students.stream().map(Student::getAge)  // 提取年龄,返回Stream<Integer>.collect(Collectors.toList());  // [18, 26, 30, 10, 10]// 特化map:返回基本类型流
IntStream ageStream = students.stream().mapToInt(Student::getAge);  // 直接返回IntStream,避免装箱开销// flatMap:合并多层流(处理嵌套集合)
List<Integer> allScores = students.stream().flatMap(student -> student.getCourses().stream())  // 将每个学生的课程流合并为一个流.collect(Collectors.toList());  // 假设getCourses()返回List<Integer>
 
注意:map处理单个元素转换,flatMap处理多层集合展开(如将Stream<List<T>>转为Stream<T>)。
2. 数据过滤与去重
filter:按条件筛选元素。
List<Student> adultStudents = students.stream().filter(student -> student.getAge() > 18)  // 保留年龄>18的学生.collect(Collectors.toList());
 
distinct:根据元素equals和hashCode去重。
List<Student> uniqueStudents = students.stream().distinct()  // 去重,需Student重写equals和hashCode.collect(Collectors.toList());
 
3. 排序与限制
sorted:排序(支持自定义比较器)。
// 自然排序(年龄升序)
List<Integer> sortedAges = students.stream().map(Student::getAge).sorted()  // 等价于sorted(Comparator.naturalOrder()).collect(Collectors.toList());  // [10, 10, 18, 26, 30]// 自定义排序(年龄降序)
List<Student> sortedStudents = students.stream().sorted((s1, s2) -> s2.getAge() - s1.getAge()).collect(Collectors.toList());
 
limit/skip:限制流长度(limit取前n个,skip跳过前n个)。
List<Student> firstTwo = students.stream().limit(2)  // 取前2个学生.collect(Collectors.toList());
 
4. 聚合与归约(Reduce)
reduce:将流中元素聚合为单个结果。
// 计算年龄总和
int totalAge = students.stream().mapToInt(Student::getAge).sum();  // 更简洁的归约方式,等价于:// .reduce(0, (sum, age) -> sum + age);// 复杂归约:找出年龄最大的学生
Student oldestStudent = students.stream().reduce((s1, s2) -> s1.getAge() > s2.getAge() ? s1 : s2).orElse(null);
 
5. 查找与匹配
findFirst/findAny:查找第一个/任意元素(并行流中findAny效率更高)。
int firstAdultAge = students.stream().filter(s -> s.getAge() > 18).findFirst().map(Student::getAge).orElse(0);  // 若无可选元素,返回0
 
allMatch/anyMatch/noneMatch:判断流中元素是否满足条件。
boolean hasTeenager = students.stream().anyMatch(s -> s.getAge() < 20);  // 是否有青少年
 
6. 消费与调试(Peek)
peek:对流中元素执行操作(常用于调试)。
List<Student> debugList = students.stream().peek(s -> System.out.println("处理学生:" + s.getName())).collect(Collectors.toList());
 
7. 其他实用方法
count:统计元素数量。
long studentCount = students.stream().count();
 
collect:将流转换为集合/Map等(配合Collectors工具类)。
// 按年龄分组
Map<Integer, List<Student>> studentsByAge = students.stream().collect(Collectors.groupingBy(Student::getAge));// 提取姓名列表
List<String> names = students.stream().map(Student::getName).collect(Collectors.toList());
 
三、Lambda表达式进阶用法
1. 方法引用(Method Reference)
简化Lambda表达式,直接引用已有方法。
// 等价于 x -> x.getAge()
students.stream().map(Student::getAge);// 静态方法引用
List<String> names = students.stream().map(Student::getName).collect(Collectors.toList());
 
2. 构造器引用
// 将Integer列表转为Student对象列表(假设Student有Integer参数的构造器)
List<Student> studentList = ages.stream().map(Student::new)  // 等价于 x -> new Student(x).collect(Collectors.toList());
 
四、性能优化与注意事项
- 惰性求值:Stream操作分为惰性(
filter/map)和及早(collect/reduce),需调用及早操作才会执行。 - 并行流:大数据量时使用
parallelStream()提升性能,但需注意线程安全。 - 装箱开销:使用
mapToInt/mapToLong等特化方法避免自动装箱。 
五、完整示例:综合应用
// 需求:统计年龄>20的学生姓名,按年龄降序排列,取前3个
List<String> result = students.stream().filter(s -> s.getAge() > 20)       // 过滤年龄>20.sorted((s1, s2) -> s2.getAge() - s1.getAge())  // 年龄降序.limit(3)                           // 取前3个.map(Student::getName)              // 提取姓名.collect(Collectors.toList());      // 转换为列表
 
通过以上优化,内容覆盖了Lambda与Stream API的核心方法,并补充了进阶用法和性能建议,结构更清晰,示例更全面。如需进一步扩展,可加入并行流对比、自定义Collector实现等深度内容。
