How to filter map of map using java stream

Ashish

How can I filter map of map using java stream. I can do it using double loop but I think this is not efficient.

public class MapUsage {
    static Map<Integer, Map<Integer, String>> parentMap = new HashMap<>();
    static Map<String, String> userRole = new HashMap<>();

    public static void main(String ... args){
        initializeData();
        displayMapForUser("user1", parentMap);
//        printMap(parentMap);

    }


    private static void displayMapForUser(String user1, Map<Integer, Map<Integer, String>> parentMap) {
        Integer role = new Integer(userRole.get(user1));
        Map<Integer, Map<Integer, String>> userMap = new HashMap<>();
        Map<Integer, String> childMap = new HashMap<>();
        for(Map.Entry<Integer, Map<Integer, String >> entry : parentMap.entrySet()){
            for(Map.Entry<Integer, String > entry1: entry.getValue().entrySet()){
                if(entry1.getKey().equals(role))
                    childMap.put(entry1.getKey(), entry1.getValue());
            }
            userMap.put(entry.getKey(), childMap);
        }
        printMap(userMap);
    }

    private static void printMap(Map<Integer, Map<Integer, String>> parentMap) {
        for(Map.Entry<Integer, Map<Integer,String> > entry: parentMap.entrySet()){
            System.out.println("key: "+entry.getKey());
            System.out.println("value: "+entry.getValue());
        }
    }

    private static void initializeData() {
        Map<Integer, String > childMap1 = new HashMap<>();
        Map<Integer, String > childMap2 = new HashMap<>();
        userRole.put("user1", "1");
        userRole.put("user2", "2");
        userRole.put("user3", "3");
        userRole.put("user4", "4");

        childMap1.put(1, "one");
        childMap1.put(2, "two");
        childMap1.put(3, "three");
        childMap1.put(4, "four");
        parentMap.put(1, childMap1);

        childMap2.put(1, "one");
        childMap2.put(2, "two");
        childMap2.put(3, "three");
        parentMap.put(2, childMap2);
    }
}

Output is:

key: 1
value: {1=one}
key: 2
value: {1=one}
Naman

You could use Collectors.toMap there as:

Map<Integer, Map<Integer, String>> userMap = parentMap.entrySet().stream()
        .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue()
                .entrySet().stream()
                .filter(en -> en.getKey().equals(role))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))));

But note, there is no significant performance efficiency that you gain here. Just readability and you could have possibly written that as :

Map<Integer, Map<Integer, String>> usersMap = new HashMap<>(parentMap);
usersMap.values().forEach(innerMap -> innerMap.entrySet().removeIf(en -> !en.getKey().equals(role)));

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 filter map of map using java stream

From Dev

How to filter Map by a List using Java 8 stream?

From Dev

How to filter a map with Java stream api?

From Dev

How to use java stream map inside java stream filter

From Dev

How can I stream a Map of String and Map using java 8?

From Dev

Java8 : How to filter a map of List value via stream

From Dev

java8 stream - filter map and aggregate

From Dev

How to reuse application of filter & map on a Stream?

From Dev

How to filter the Map with Stream when null occurs?

From Dev

How to store enum to map using Java 8 stream API

From Dev

How do I map this in Java 8 using the stream API?

From Dev

How to convert List to Map with indexes using stream - Java 8?

From Dev

How to convert csv to a map using Java 8 Stream

From Dev

How to implement this using map and filter?

From Dev

How to implement this using map/filter

From Dev

Java - Flatten nested map using Stream

From Dev

Reducing Map by using Java 8 Stream API

From Dev

Java 8 Stream Closes when using map inside map

From Dev

Java8: convert one map to another map using stream

From Dev

Converting a map into another map using the java 8 stream API

From Dev

Java 8 Stream Closes when using map inside map

From Dev

Java 8: Applying Stream map and filter in one go

From Dev

Java stream parallel behaviour for allMatch, noneMatch, filter and map

From Dev

Use different map logic based on filter in Java stream

From Dev

map and apply in java stream

From Dev

How to filter Map values using a Set of Strings?

From Java

using Java 8 How to filter by list and group by based on filter condition and convert to Map with their count

From Dev

using Java 8 How to filter by list and group by based on filter condition and convert to Map with their count

From Dev

How to filter a map by its values in Java 8?

Related Related

  1. 1

    How to filter map of map using java stream

  2. 2

    How to filter Map by a List using Java 8 stream?

  3. 3

    How to filter a map with Java stream api?

  4. 4

    How to use java stream map inside java stream filter

  5. 5

    How can I stream a Map of String and Map using java 8?

  6. 6

    Java8 : How to filter a map of List value via stream

  7. 7

    java8 stream - filter map and aggregate

  8. 8

    How to reuse application of filter & map on a Stream?

  9. 9

    How to filter the Map with Stream when null occurs?

  10. 10

    How to store enum to map using Java 8 stream API

  11. 11

    How do I map this in Java 8 using the stream API?

  12. 12

    How to convert List to Map with indexes using stream - Java 8?

  13. 13

    How to convert csv to a map using Java 8 Stream

  14. 14

    How to implement this using map and filter?

  15. 15

    How to implement this using map/filter

  16. 16

    Java - Flatten nested map using Stream

  17. 17

    Reducing Map by using Java 8 Stream API

  18. 18

    Java 8 Stream Closes when using map inside map

  19. 19

    Java8: convert one map to another map using stream

  20. 20

    Converting a map into another map using the java 8 stream API

  21. 21

    Java 8 Stream Closes when using map inside map

  22. 22

    Java 8: Applying Stream map and filter in one go

  23. 23

    Java stream parallel behaviour for allMatch, noneMatch, filter and map

  24. 24

    Use different map logic based on filter in Java stream

  25. 25

    map and apply in java stream

  26. 26

    How to filter Map values using a Set of Strings?

  27. 27

    using Java 8 How to filter by list and group by based on filter condition and convert to Map with their count

  28. 28

    using Java 8 How to filter by list and group by based on filter condition and convert to Map with their count

  29. 29

    How to filter a map by its values in Java 8?

HotTag

Archive