
本教程介绍了如何使用 Java Stream 将从多个 CSV 文件中读取的数据进行合并。重点在于保持读取顺序,并提供了一个基于城市和国家数据的示例,展示了如何通过 `forEach` 和 `filter` 操作来实现数据关联和合并。
在实际开发中,经常会遇到需要从多个数据源(例如多个 CSV 文件)读取数据,并将这些数据进行关联和合并的需求。 本文将介绍一种使用 Java Stream 来实现这种数据合并的方法,特别是在需要保持原始数据顺序的情况下。
场景描述
假设我们有两个 CSV 文件,分别存储城市(City)和国家(Country)的数据。我们需要根据城市数据中的国家代码(countryCode)将城市数据和国家数据关联起来,并将关联后的数据合并。 目标是为每个城市对象添加对应的国家名称,并且保持城市数据在原始 CSV 文件中的顺序。
立即学习“Java免费学习笔记(深入)”;
数据模型
首先,定义两个 Java 类来表示城市和国家的数据结构:
import com.opencsv.bean.CsvBindByPosition;
public class City {
@CsvBindByPosition(position = 0)
private Integer id;
@CsvBindByPosition(position = 1)
private String name;
@CsvBindByPosition(position = 2)
private String countryCode;
private String countryName;
// Getters and setters
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
@Override
public String toString() {
return "City{" +
"id=" + id +
", name='" + name + '\'' +
", countryCode='" + countryCode + '\'' +
", countryName='" + countryName + '\'' +
'}';
}
}import com.opencsv.bean.CsvBindByPosition;
public class Country {
@CsvBindByPosition(position = 0)
private Integer id;
@CsvBindByPosition(position = 1)
private String name;
@CsvBindByPosition(position = 2)
private String code;
// Getters and setters
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public String toString() {
return "Country{" +
"id=" + id +
", name='" + name + '\'' +
", code='" + code + '\'' +
'}';
}
}数据读取和合并
接下来,展示如何使用 Java Stream 来读取 CSV 文件中的数据,并将城市数据和国家数据进行合并。 假设我们已经从 CSV 文件中读取了城市和国家的数据,并分别存储在 cities 和 countries 列表中。
import java.util.Arrays;
import java.util.List;
public class StreamMergeExample {
public static void main(String[] args) {
// 模拟从CSV读取的数据
List countries = Arrays.asList(
new Country(100, "Germany", "DE"),
new Country(105, "France", "FR"),
new Country(108, "Denmark", "DK")
);
List cities = Arrays.asList(
new City(1, "Berlin", "DE"),
new City(2, "Munich", "DE"),
new City(3, "Köln", "DE"),
new City(4, "Paris", "FR"),
new City(5, "Kopenhag", "DK")
);
// 使用 Stream 合并数据
cities.forEach(city -> city.setCountryName(countries.stream()
.filter(country -> country.getCode().equals(city.getCountryCode()))
.map(Country::getName)
.findAny()
.orElse(null)));
// 打印合并后的城市数据
cities.forEach(System.out::println);
}
} 代码解释
- cities.forEach(...): 使用 forEach 方法遍历城市列表,确保按照原始顺序处理每个城市对象。
- countries.stream(): 为每个城市创建一个国家列表的 Stream。
- .filter(country -> country.getCode().equals(city.getCountryCode())): 使用 filter 方法筛选出与当前城市国家代码匹配的国家对象。
- .map(Country::getName): 使用 map 方法提取国家名称。
- .findAny().orElse(null): 使用 findAny 方法获取匹配的第一个国家名称。如果未找到匹配项,则返回 null。
- city.setCountryName(...): 将获取到的国家名称设置到城市对象的 countryName 属性中。
注意事项
- 上述代码使用了 findAny() 方法。 如果需要确保返回唯一的结果,并且知道国家代码在国家列表中是唯一的,可以使用 findFirst() 方法代替。
- 如果需要处理更复杂的数据关联逻辑,例如一对多关系,可能需要使用 flatMap 等其他 Stream 操作。
- 如果 CSV 文件非常大,可以考虑使用分页读取等方式来避免一次性加载大量数据到内存中。
总结
本文介绍了一种使用 Java Stream 将从多个 CSV 文件中读取的数据进行合并的方法。通过 forEach 循环和 Stream 操作,可以方便地实现数据关联和合并,并且保持原始数据的顺序。这种方法适用于需要处理多个数据源,并将数据进行关联和合并的场景。










