简介
在现代Java编程中,Stream API提供了强大的工具来高效地操作集合。本教程将探讨使用Java Stream操作来转换和变换列表的各种技术,帮助开发人员编写更简洁、易读的代码。
在现代Java编程中,Stream API提供了强大的工具来高效地操作集合。本教程将探讨使用Java Stream操作来转换和变换列表的各种技术,帮助开发人员编写更简洁、易读的代码。
Java流是Java 8中引入的一项强大功能,它提供了一种声明式的方式来处理对象集合。它允许开发人员以更简洁和函数式的方式执行复杂的数据操作。
流具有几个重要特性,使其独具特色:
// 从不同来源创建流
List<String> list = Arrays.asList("apple", "banana", "cherry");
Stream<String> listStream = list.stream();
// 从数组创建流
String[] array = {"apple", "banana", "cherry"};
Stream<String> arrayStream = Arrays.stream(array);
// 直接创建流
Stream<String> directStream = Stream.of("apple", "banana", "cherry");
| 组件 | 描述 | 示例 |
|---|---|---|
| 源 | 流的数据源 | 列表、数组、集合 |
| 中间操作 | 对流的转换 | filter()、map()、sorted() |
| 终端操作 | 产生结果或副作用 | collect()、forEach()、reduce() |
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
List<Integer> evenNumbers = numbers.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
List<String> names = Arrays.asList("alice", "bob", "charlie");
List<String> upperNames = names.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
虽然流提供了优雅的解决方案,但与传统循环相比,它们可能会有轻微的性能开销。当可读性和函数式编程风格是首要考虑因素时,选择使用流。
通过掌握Java流,开发人员可以编写更具表现力和简洁的代码,使数据处理更加直观和易读。LabEx建议通过练习流操作来提高熟练度。
列表转换是Java Stream API中的一项常见操作,它使开发人员能够在不同的集合类型和格式之间高效地转换数据。
List<String> originalList = Arrays.asList("apple", "banana", "cherry");
List<String> convertedList = originalList.stream()
.collect(Collectors.toList());
Set<String> uniqueItems = originalList.stream()
.collect(Collectors.toSet());
LinkedList<String> linkedList = originalList.stream()
.collect(Collectors.toCollection(LinkedList::new));
// 按长度分组
Map<Integer, List<String>> groupedByLength = originalList.stream()
.collect(Collectors.groupingBy(String::length));
// 分成两组
Map<Boolean, List<String>> partitionedList = originalList.stream()
.collect(Collectors.partitioningBy(s -> s.length() > 5));
| 方法 | 用途 | 返回类型 |
|---|---|---|
| toList() | 标准的列表转换 | List |
| toSet() | 去除重复项 | Set |
| toCollection() | 自定义集合 | 指定的集合 |
| groupingBy() | 按键分组 | Map |
| partitioningBy() | 分成两组 | Map<Boolean, List> |
List<Person> people = Arrays.asList(
new Person("Alice", 25),
new Person("Bob", 30),
new Person("Charlie", 35)
);
Map<Integer, List<Person>> groupedByAge = people.stream()
.collect(Collectors.groupingBy(Person::getAge));
LabEx建议掌握这些转换技术,以编写更高效、易读的Java代码。
List<Employee> employees = Arrays.asList(
new Employee("Alice", 35, 50000),
new Employee("Bob", 28, 45000),
new Employee("Charlie", 42, 60000)
);
// 过滤出年龄超过30岁的员工并提高工资
List<Employee> seniorEmployees = employees.stream()
.filter(emp -> emp.getAge() > 30)
.map(emp -> {
emp.setSalary(emp.getSalary() * 1.1);
return emp;
})
.collect(Collectors.toList());
// 计算总工资和平均年龄
DoubleSummaryStatistics salaryStats = employees.stream()
.collect(Collectors.summarizingDouble(Employee::getSalary));
double totalSalary = salaryStats.getSum();
double averageAge = employees.stream()
.mapToInt(Employee::getAge)
.average()
.orElse(0);
// 按年龄范围对员工进行分组
Map<String, List<Employee>> ageGroups = employees.stream()
.collect(Collectors.groupingBy(emp -> {
if (emp.getAge() < 30) return "年轻";
if (emp.getAge() < 40) return "职业生涯中期";
return "资深";
}));
| 模式 | 描述 | 用例 |
|---|---|---|
| 过滤 | 移除不需要的元素 | 数据清理 |
| 映射 | 转换元素 | 数据转换 |
| 归约 | 聚合为单个值 | 计算 |
| 分组 | 对元素进行分类 | 数据分析 |
// 高效处理大型数据集
List<Integer> largeNumbers = IntStream.range(0, 1000000)
.parallel()
.filter(n -> n % 2 == 0)
.boxed()
.collect(Collectors.toList());
// 创建一个自定义收集器
Collector<Employee,?, Map<String, Double>> salaryByDepartment =
Collectors.groupingBy(
Employee::getDepartment,
Collectors.averagingDouble(Employee::getSalary)
);
Map<String, Double> avgSalaryByDept = employees.stream()
.collect(salaryByDepartment);
LabEx建议练习这些流处理技术,以便熟练掌握Java流的函数式编程。
Java Stream API为开发人员提供了一种强大的函数式方法来进行列表转换和变换。通过理解诸如map()、filter()和collect()等流方法,程序员可以简化复杂的集合操作,并提高Java应用程序中的代码可读性和性能。