【问题标题】:How to find intersection for two streams with java 8?java - 如何使用java 8找到两个流的交集?
【发布时间】:2016-10-12 14:48:32
【问题描述】:

我有以下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 功能实现相同的功能,例如流?

【问题讨论】:

  • 我认为您最好使用MultiSet,流放在一边。除了明显的Files.lines 而不是Files.readAllLines,您希望获得什么?

标签: java csv enums java-8 java-stream


【解决方案1】:

我们可以:

例如:

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]);
}

【讨论】:

  • 您的解决方案应该可以正常工作。但是,我尝试了它并得到了不同于迭代方法的另一个结果。可以理解为什么会这样。
猜你喜欢
  • 2019-04-17
  • 2014-01-06
  • 2013-07-25
  • 2014-05-01
  • 2015-10-19
  • 2013-01-14
  • 2017-07-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多