How to find intersection for two streams with java 8?

catch23

I have following csv file:

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

I need to check how many times arrival and departure countries are mentioned there. And use for this functional approach. CSV file has only country codes. All countries are stored at predefined enum.

My solution works iteratively definitely, I am sure that it can be implemented with streams. I tried to play with collect() & groupingBy() but without success.

Here is iterative solution (result is stored to map with country for key - number of occurrences of values):

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 is enum for storing relative pathes for csv files.

Here you can find Country enum

It works fine:

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

How to achieve the same with Java 8 features, like streams?

Daniel Rodriguez

We can:

For example:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to compare two Streams in Java 8

From Dev

How to interleave (merge) two Java 8 Streams?

From Dev

How to search between two Streams in Java 8

From Dev

Intersection of two List<int[]> types by java streams

From Dev

Intersection between nested lists java 8 streams

From Dev

Java, find intersection of two arrays

From Dev

Java find intersection of two lines

From Dev

How to find the intersection of two NFA

From Dev

Combine two streams Java 8

From Dev

How to get the symmetric difference between two Streams in Java 8?

From Dev

How do I append two Streams in Java 8?

From Dev

How to get the symmetric difference between two Streams in Java 8?

From Dev

Using Java 8 Streams, how to find the max for a given element in a HashMap

From Dev

Java 8 Lambda - Intersection of Two Lists

From Dev

How do you find intersection of two functions

From Dev

How to find intersection of two distribution in Matlab

From Dev

How to find intersection of two arrays (Optimal solution)

From Dev

Microsoft Excel: How to find the intersection of two lines?

From Dev

How to merge two streams in Java?

From Dev

How to work with Java 8 streams?

From Dev

Iterate two Java-8-Streams together

From Dev

Refactor two for's into java 8 streams

From Dev

Java 8 streams - filter by comparing two lists

From Dev

Find intersection of two Locations

From Dev

Intersection of two collections of different objects types java 8

From Dev

Intersection of two collections of different objects types java 8

From Dev

How to implement Java 8 generic method that returns zipped stream where objects from two streams appear in turn?

From Dev

How to use Java 8 streams to find all values preceding a larger value?

From Java

How to find max key with some conditions in HashMap using Java 8 / Streams?

Related Related

  1. 1

    How to compare two Streams in Java 8

  2. 2

    How to interleave (merge) two Java 8 Streams?

  3. 3

    How to search between two Streams in Java 8

  4. 4

    Intersection of two List<int[]> types by java streams

  5. 5

    Intersection between nested lists java 8 streams

  6. 6

    Java, find intersection of two arrays

  7. 7

    Java find intersection of two lines

  8. 8

    How to find the intersection of two NFA

  9. 9

    Combine two streams Java 8

  10. 10

    How to get the symmetric difference between two Streams in Java 8?

  11. 11

    How do I append two Streams in Java 8?

  12. 12

    How to get the symmetric difference between two Streams in Java 8?

  13. 13

    Using Java 8 Streams, how to find the max for a given element in a HashMap

  14. 14

    Java 8 Lambda - Intersection of Two Lists

  15. 15

    How do you find intersection of two functions

  16. 16

    How to find intersection of two distribution in Matlab

  17. 17

    How to find intersection of two arrays (Optimal solution)

  18. 18

    Microsoft Excel: How to find the intersection of two lines?

  19. 19

    How to merge two streams in Java?

  20. 20

    How to work with Java 8 streams?

  21. 21

    Iterate two Java-8-Streams together

  22. 22

    Refactor two for's into java 8 streams

  23. 23

    Java 8 streams - filter by comparing two lists

  24. 24

    Find intersection of two Locations

  25. 25

    Intersection of two collections of different objects types java 8

  26. 26

    Intersection of two collections of different objects types java 8

  27. 27

    How to implement Java 8 generic method that returns zipped stream where objects from two streams appear in turn?

  28. 28

    How to use Java 8 streams to find all values preceding a larger value?

  29. 29

    How to find max key with some conditions in HashMap using Java 8 / Streams?

HotTag

Archive