gpt4 book ai didi

java - 如何使用 Java 8 查找两个流的交集?

转载 作者:行者123 更新时间:2023-11-30 06:51:37 25 4
gpt4 key购买 nike

我有以下 csv 文件:

OF_DEPARTURE_COORDINATE_Y,OF_ARRIVAL_COUNTRY,OF_ARRLV2,OF_ARRLV1,OF_ARRLV0,OF_ARRIVAL_CITY,OF_ARRIVAL_ZIPCODE,OF_ARRIVAL_COORDINATE_X,OF_ARRIVAL_COORDINATE_Y,OF_WEIGHT,OF_VOLUME,OF_LENGTH,OF_GOODS_KND,OF_TAIL_LIFT,OF_PALLETS_EXCHANGE,OF_NB_PALLETS
D,SN,1,,DRESDEN,01067,1372931,5105325,A,3,SB,57,ZELL AM SEE,5700,1279591,4732422,2500,0,36,MG,N,N,0
D,HE,35,,HAIGER,35708,820051,5074357,RO,2,,,ORADEA,410000,2193891,4705371,100,1,0,MG,N,N,0
F,NP,62,,ANVIN,62134,225617,5044640,F,BR,29,,QUIMPER,29000,-410790,4799464,10000,0,50,MG,N,N,0

我需要检查那里提到了多少次到达和离开国家。并用于这种功能性方法。 CSV 文件只有国家代码。所有国家/地区都存储在预定义的枚举中。

我的解决方案肯定是迭代工作的,我确信它可以用流来实现。我尝试使用 collect()groupingBy() 但没有成功。

这是迭代解决方案(结果存储为与国家/地区映射的键 - 值的出现次数):

public class CountryCounter {
private static Map<Country, Long> countryMap = Country.getCountryMap();

public static void main(String[] args) {
processPath(FileLocation.SEARCHES_REG);
printMap();
}

private static void printMap() {
Map<Country, Long> reversedMap = new TreeMap<>(countryMap);
Map<Country, Long> result = new LinkedHashMap<>();

reversedMap.entrySet().stream()
.sorted(Map.Entry.<Country, Long>comparingByValue().reversed())
.forEachOrdered(x -> result.put(x.getKey(), x.getValue()));

for (Map.Entry entry : result.entrySet()) {
System.out.println(entry.getKey() + ", " + entry.getValue());
}
}

private static void processPath(FileLocation filePath) {
FileLocation.printFileName(filePath);

Path path = Paths.get(".", filePath.getFilePath());

List<String> csvLines = null;
try {
csvLines = Files.readAllLines(path);
} catch (IOException e) {
e.printStackTrace();
}

for (String csvLine : csvLines) {
String[] lineArgs = csvLine.split(",");
String arrivalCntCode = lineArgs[0];
String departureCntCode = lineArgs[8];

if (arrivalCntCode == null || departureCntCode == null) {
return;
}

Country arrCountry = Country.getByCode(arrivalCntCode);
Country depCountry = Country.getByCode(departureCntCode);

if (countryMap.containsKey(arrCountry)) {
countryMap.put(arrCountry, countryMap.get(arrCountry) + 1);
}
if (countryMap.containsKey(depCountry)) {
countryMap.put(depCountry, countryMap.get(depCountry) + 1);
}
}
}
}

FileLocation 是用于存储 csv 文件的相对路径的枚举。

在这里你可以找到Country enum

它工作正常:

France, 82109
Germany, 31589
Romania, 27634
Italy, 11652
Netherlands, 9190
...

如何使用 Java 8 功能实现同样的效果,例如流?

最佳答案

我们可以:

例如:

public Map<Country, Long> count(Path path) throws IOException {
return Files.lines(path)
.flatMap(line -> getRelevantCells(line))
.map(Country::getByCode)
.filter(Objects::nonNull)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
}

private Stream<String> getRelevantCells(String line) {
String[] cells = line.split(",");
return Stream.of(cells[0], cells[8]);
}

关于java - 如何使用 Java 8 查找两个流的交集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40001692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com