
本文介绍了如何使用 Java Stream API 将从两个 CSV 文件中读取的城市和国家数据进行关联。通过遍历城市列表,并使用 Stream API 在国家列表中查找匹配的国家代码,最终将国家名称添加到对应的城市对象中,保持原始城市列表的顺序。
在实际应用中,我们经常需要从多个数据源(例如 CSV 文件)读取数据,并将它们按照某种规则进行关联。本文以城市和国家数据为例,演示如何使用 Java Stream API 实现高效的数据合并,并保持原始数据的顺序。
数据模型
首先,定义两个 Java 类 City 和 Country 来表示城市和国家的数据结构。这两个类都使用了 @CsvBindByPosition 注解,该注解来自 opencsv 库,用于将 CSV 文件中的列绑定到类的属性。
立即学习“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 + '\'' +
'}';
}
}数据读取与关联
接下来,假设我们已经从 CSV 文件中读取了城市和国家的数据,并将它们分别存储在 List<City> 和 List<Country> 类型的变量 cities 和 countries 中。
以下代码展示了如何使用 Java Stream API 将城市和国家数据进行关联,并将国家名称设置到对应的城市对象中:
import java.util.Arrays;
import java.util.List;
public class StreamMerge {
public static void main(String[] args) {
// 模拟从CSV读取的数据
List<Country> countries = Arrays.asList(
new Country(100, "Germany", "DE"),
new Country(105, "France", "FR"),
new Country(108, "Denmark", "DK")
);
List<City> 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 API 关联数据
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);
}
}
代码解释
注意事项
总结
本文展示了如何使用 Java Stream API 将从多个 CSV 文件中读取的数据进行关联。通过使用 Stream API 的 filter、map 和 findAny 方法,可以高效地实现数据合并,并保持原始数据的顺序。在实际应用中,需要根据具体情况选择合适的数据结构和算法,并进行适当的异常处理,以确保程序的正确性和性能。
以上就是Java Stream 实现 CSV 数据合并:城市与国家信息关联的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号