
本文介绍如何使用 Java Stream 将从多个 CSV 文件读取的数据进行合并,并保持与第一个数据集相同的顺序。核心思路是利用 `forEach` 方法遍历第一个数据集(例如城市列表),然后在 Stream 中查找与第二个数据集(例如国家列表)匹配的记录,并将匹配到的信息添加到第一个数据集的相应对象中。
在实际应用中,我们经常需要从多个数据源(例如 CSV 文件)读取数据,并将这些数据进行关联和合并。如果需要保持特定数据集的顺序,同时又希望利用 Java Stream 的强大功能,就需要一种高效且可靠的方法。以下将详细介绍如何实现这一目标。
首先,定义两个实体类 City 和 Country,分别对应城市和国家的数据结构。这两个类都使用 CsvBindByPosition 注解,用于将 CSV 文件中的列绑定到类的属性上。
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;
}
}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;
}
}注意,我们在 City 类中添加了一个 countryName 属性,用于存储从 Country 数据集中匹配到的国家名称。
立即学习“Java免费学习笔记(深入)”;
以下代码演示了如何使用 Java Stream 将城市和国家数据进行合并,并保持城市数据的原始顺序:
import java.util.List;
public class DataMerger {
public static void mergeCityAndCountry(List<City> cities, List<Country> countries) {
cities.forEach(city -> city.setCountryName(countries.stream()
.filter(country -> country.getCode().equals(city.getCountryCode()))
.map(Country::getName)
.findAny()
.orElse(null)));
}
public static void main(String[] args) {
// 模拟从 CSV 文件读取数据
List<City> cities = List.of(
new City() {{ setId(1); setName("Berlin"); setCountryCode("DE"); }},
new City() {{ setId(5); setName("Kopenhag"); setCountryCode("DK"); }},
new City() {{ setId(4); setName("Paris"); setCountryCode("FR"); }}
);
List<Country> countries = List.of(
new Country() {{ setId(100); setName("Germany"); setCode("DE"); }},
new Country() {{ setId(105); setName("France"); setCode("FR"); }},
new Country() {{ setId(108); setName("Denmark"); setCode("DK"); }}
);
mergeCityAndCountry(cities, countries);
// 打印合并后的结果
cities.forEach(city -> System.out.println(city.getName() + " - " + city.getCountryName()));
}
}代码解释:
通过使用 Java Stream 的 forEach 方法和 filter 方法,我们可以方便地将从多个 CSV 文件读取的数据进行合并,并保持特定数据集的原始顺序。这种方法简洁、高效,并且易于理解和维护。在实际应用中,可以根据具体情况进行适当的优化和改进。
以上就是Java Stream:合并从多个 CSV 文件读取的结果的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号