Java 8 Strem filter map in map — Map<String,Map>












4














How to filter a Map<String,Map<String,Employee>> using Java 8 Filter?



I have to filter only when any of employee in the list having a field value Gender = "M".



Input:




Map<String,Map<String,Employee>>




Output:




Map<String,Map<String,Employee>>




Filter criteria:




Employee.genter = "M"




Also i have to return empty map if the filtered result is empty.



I tried the below, but it is not working as expected. It is returning the only if all the Employees are with gender "M".



tempCollection.entrySet().stream()
.filter(i -> i.getValue().entrySet().stream().allMatch(e-> "M".equals(e.getValue().getGender())))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));









share|improve this question
























  • Return all the employees whose gender is M.
    – user1578872
    13 hours ago
















4














How to filter a Map<String,Map<String,Employee>> using Java 8 Filter?



I have to filter only when any of employee in the list having a field value Gender = "M".



Input:




Map<String,Map<String,Employee>>




Output:




Map<String,Map<String,Employee>>




Filter criteria:




Employee.genter = "M"




Also i have to return empty map if the filtered result is empty.



I tried the below, but it is not working as expected. It is returning the only if all the Employees are with gender "M".



tempCollection.entrySet().stream()
.filter(i -> i.getValue().entrySet().stream().allMatch(e-> "M".equals(e.getValue().getGender())))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));









share|improve this question
























  • Return all the employees whose gender is M.
    – user1578872
    13 hours ago














4












4








4


1





How to filter a Map<String,Map<String,Employee>> using Java 8 Filter?



I have to filter only when any of employee in the list having a field value Gender = "M".



Input:




Map<String,Map<String,Employee>>




Output:




Map<String,Map<String,Employee>>




Filter criteria:




Employee.genter = "M"




Also i have to return empty map if the filtered result is empty.



I tried the below, but it is not working as expected. It is returning the only if all the Employees are with gender "M".



tempCollection.entrySet().stream()
.filter(i -> i.getValue().entrySet().stream().allMatch(e-> "M".equals(e.getValue().getGender())))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));









share|improve this question















How to filter a Map<String,Map<String,Employee>> using Java 8 Filter?



I have to filter only when any of employee in the list having a field value Gender = "M".



Input:




Map<String,Map<String,Employee>>




Output:




Map<String,Map<String,Employee>>




Filter criteria:




Employee.genter = "M"




Also i have to return empty map if the filtered result is empty.



I tried the below, but it is not working as expected. It is returning the only if all the Employees are with gender "M".



tempCollection.entrySet().stream()
.filter(i -> i.getValue().entrySet().stream().allMatch(e-> "M".equals(e.getValue().getGender())))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));






java java-8 hashmap java-stream






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 12 hours ago









Aomine

38.7k73365




38.7k73365










asked 13 hours ago









user1578872

1,53452563




1,53452563












  • Return all the employees whose gender is M.
    – user1578872
    13 hours ago


















  • Return all the employees whose gender is M.
    – user1578872
    13 hours ago
















Return all the employees whose gender is M.
– user1578872
13 hours ago




Return all the employees whose gender is M.
– user1578872
13 hours ago












5 Answers
5






active

oldest

votes


















3














You could simply iterate on the key-value pairs and filter as:



Map<String, Map<String, Employee>> output = new HashMap<>();
tempCollection.forEach((k, v) -> {
if (v.values().stream().anyMatch(i -> "M".equals(i.getGender()))) {
output.put(k, v.entrySet()
.stream()
.filter(i -> "M".equals(i.getValue().getGender()))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue)));
}
});





share|improve this answer























  • anyMatch returns all the result even there is a single match, I want the key value to be removed if there is no match,
    – user1578872
    13 hours ago










  • If none of the employee is with the Gender "M", it still returns Map<String,emptyMap()>
    – user1578872
    12 hours ago










  • @user1578872 i have to return empty map if the filtered result is empty... that's what you meant, right?
    – nullpointer
    12 hours ago












  • It should be completeEmpty map in this case. Like, return emptyMap(), not Map<String,emptyMap()>
    – user1578872
    12 hours ago






  • 2




    I think put would be more ideal rather than putIfAbsent as the key will always be unique.
    – Aomine
    12 hours ago





















0














The function allMatch only matches if every element in the stream matches the predicate; you can use anyMatch to match if any element matches the predicate:



tempCollection.entrySet().stream()
.filter(i -> i.getValue().entrySet().stream().anyMatch(e-> "M".equals(e.getValue().getGender())))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));





share|improve this answer





























    0














    Seems like what you're after is given a Entry<String,Map<String,Employee>> if there's any employee who has a gender of "M" then filter the inner Map<String,Employee> to contain only entries with a gender "M".



    in which case you can filter along with anyMatch for the first criterion. Further, at the collecting phase, you can then apply the filtering on the inner map:



    tempCollection.entrySet().stream()
    .filter(i -> i.getValue().values().stream().anyMatch(e -> "M".equals(e.getGender())))
    .collect(toMap(Map.Entry::getKey,
    v -> v.getValue().entrySet().stream()
    .filter(i -> "M".equals(i.getValue().getGender()))
    .collect(toMap(Map.Entry::getKey, Map.Entry::getValue))));





    share|improve this answer























    • anyMatch returns all the values in the map even there is only one employee with Gender M.
      – user1578872
      12 hours ago










    • @user1578872 right, thanks to the accepted answer, I can now understand your requirement. the description wasn't very clear IMO. anyway edited to accommodate.
      – Aomine
      12 hours ago





















    0














    Other way would be like this:



    map.values()
    .removeIf(entry->entry.values().stream().anyMatch(e -> !"M".equals(e.getGender())));

    map.entrySet()
    .removeIf(entry->entry.getValue().size() == 0);





    share|improve this answer





























      0














      You may do it like so,



      Map<String, Map<String, Employee>> maleEmpMap = tempCollection.entrySet().stream()
      .map(e -> new AbstractMap.SimpleEntry<>(e.getKey(),
      e.getValue().entrySet().stream().filter(emp -> "M".equals(emp.getValue().getGender()))
      .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))))
      .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));


      Take each map entry from the outer map and create a new map entry using the same key and the newly constructed nested map with the same key and which has only male employees as it's value. Finally collect all those matching entries into a new outer map. This will gracefully handle the empty scenario which you mentioned in the problem statement above.






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53927656%2fjava-8-strem-filter-map-in-map-mapstring-mapstring-employee%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        3














        You could simply iterate on the key-value pairs and filter as:



        Map<String, Map<String, Employee>> output = new HashMap<>();
        tempCollection.forEach((k, v) -> {
        if (v.values().stream().anyMatch(i -> "M".equals(i.getGender()))) {
        output.put(k, v.entrySet()
        .stream()
        .filter(i -> "M".equals(i.getValue().getGender()))
        .collect(toMap(Map.Entry::getKey, Map.Entry::getValue)));
        }
        });





        share|improve this answer























        • anyMatch returns all the result even there is a single match, I want the key value to be removed if there is no match,
          – user1578872
          13 hours ago










        • If none of the employee is with the Gender "M", it still returns Map<String,emptyMap()>
          – user1578872
          12 hours ago










        • @user1578872 i have to return empty map if the filtered result is empty... that's what you meant, right?
          – nullpointer
          12 hours ago












        • It should be completeEmpty map in this case. Like, return emptyMap(), not Map<String,emptyMap()>
          – user1578872
          12 hours ago






        • 2




          I think put would be more ideal rather than putIfAbsent as the key will always be unique.
          – Aomine
          12 hours ago


















        3














        You could simply iterate on the key-value pairs and filter as:



        Map<String, Map<String, Employee>> output = new HashMap<>();
        tempCollection.forEach((k, v) -> {
        if (v.values().stream().anyMatch(i -> "M".equals(i.getGender()))) {
        output.put(k, v.entrySet()
        .stream()
        .filter(i -> "M".equals(i.getValue().getGender()))
        .collect(toMap(Map.Entry::getKey, Map.Entry::getValue)));
        }
        });





        share|improve this answer























        • anyMatch returns all the result even there is a single match, I want the key value to be removed if there is no match,
          – user1578872
          13 hours ago










        • If none of the employee is with the Gender "M", it still returns Map<String,emptyMap()>
          – user1578872
          12 hours ago










        • @user1578872 i have to return empty map if the filtered result is empty... that's what you meant, right?
          – nullpointer
          12 hours ago












        • It should be completeEmpty map in this case. Like, return emptyMap(), not Map<String,emptyMap()>
          – user1578872
          12 hours ago






        • 2




          I think put would be more ideal rather than putIfAbsent as the key will always be unique.
          – Aomine
          12 hours ago
















        3












        3








        3






        You could simply iterate on the key-value pairs and filter as:



        Map<String, Map<String, Employee>> output = new HashMap<>();
        tempCollection.forEach((k, v) -> {
        if (v.values().stream().anyMatch(i -> "M".equals(i.getGender()))) {
        output.put(k, v.entrySet()
        .stream()
        .filter(i -> "M".equals(i.getValue().getGender()))
        .collect(toMap(Map.Entry::getKey, Map.Entry::getValue)));
        }
        });





        share|improve this answer














        You could simply iterate on the key-value pairs and filter as:



        Map<String, Map<String, Employee>> output = new HashMap<>();
        tempCollection.forEach((k, v) -> {
        if (v.values().stream().anyMatch(i -> "M".equals(i.getGender()))) {
        output.put(k, v.entrySet()
        .stream()
        .filter(i -> "M".equals(i.getValue().getGender()))
        .collect(toMap(Map.Entry::getKey, Map.Entry::getValue)));
        }
        });






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 1 hour ago

























        answered 13 hours ago









        nullpointer

        40.9k1085168




        40.9k1085168












        • anyMatch returns all the result even there is a single match, I want the key value to be removed if there is no match,
          – user1578872
          13 hours ago










        • If none of the employee is with the Gender "M", it still returns Map<String,emptyMap()>
          – user1578872
          12 hours ago










        • @user1578872 i have to return empty map if the filtered result is empty... that's what you meant, right?
          – nullpointer
          12 hours ago












        • It should be completeEmpty map in this case. Like, return emptyMap(), not Map<String,emptyMap()>
          – user1578872
          12 hours ago






        • 2




          I think put would be more ideal rather than putIfAbsent as the key will always be unique.
          – Aomine
          12 hours ago




















        • anyMatch returns all the result even there is a single match, I want the key value to be removed if there is no match,
          – user1578872
          13 hours ago










        • If none of the employee is with the Gender "M", it still returns Map<String,emptyMap()>
          – user1578872
          12 hours ago










        • @user1578872 i have to return empty map if the filtered result is empty... that's what you meant, right?
          – nullpointer
          12 hours ago












        • It should be completeEmpty map in this case. Like, return emptyMap(), not Map<String,emptyMap()>
          – user1578872
          12 hours ago






        • 2




          I think put would be more ideal rather than putIfAbsent as the key will always be unique.
          – Aomine
          12 hours ago


















        anyMatch returns all the result even there is a single match, I want the key value to be removed if there is no match,
        – user1578872
        13 hours ago




        anyMatch returns all the result even there is a single match, I want the key value to be removed if there is no match,
        – user1578872
        13 hours ago












        If none of the employee is with the Gender "M", it still returns Map<String,emptyMap()>
        – user1578872
        12 hours ago




        If none of the employee is with the Gender "M", it still returns Map<String,emptyMap()>
        – user1578872
        12 hours ago












        @user1578872 i have to return empty map if the filtered result is empty... that's what you meant, right?
        – nullpointer
        12 hours ago






        @user1578872 i have to return empty map if the filtered result is empty... that's what you meant, right?
        – nullpointer
        12 hours ago














        It should be completeEmpty map in this case. Like, return emptyMap(), not Map<String,emptyMap()>
        – user1578872
        12 hours ago




        It should be completeEmpty map in this case. Like, return emptyMap(), not Map<String,emptyMap()>
        – user1578872
        12 hours ago




        2




        2




        I think put would be more ideal rather than putIfAbsent as the key will always be unique.
        – Aomine
        12 hours ago






        I think put would be more ideal rather than putIfAbsent as the key will always be unique.
        – Aomine
        12 hours ago















        0














        The function allMatch only matches if every element in the stream matches the predicate; you can use anyMatch to match if any element matches the predicate:



        tempCollection.entrySet().stream()
        .filter(i -> i.getValue().entrySet().stream().anyMatch(e-> "M".equals(e.getValue().getGender())))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));





        share|improve this answer


























          0














          The function allMatch only matches if every element in the stream matches the predicate; you can use anyMatch to match if any element matches the predicate:



          tempCollection.entrySet().stream()
          .filter(i -> i.getValue().entrySet().stream().anyMatch(e-> "M".equals(e.getValue().getGender())))
          .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));





          share|improve this answer
























            0












            0








            0






            The function allMatch only matches if every element in the stream matches the predicate; you can use anyMatch to match if any element matches the predicate:



            tempCollection.entrySet().stream()
            .filter(i -> i.getValue().entrySet().stream().anyMatch(e-> "M".equals(e.getValue().getGender())))
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));





            share|improve this answer












            The function allMatch only matches if every element in the stream matches the predicate; you can use anyMatch to match if any element matches the predicate:



            tempCollection.entrySet().stream()
            .filter(i -> i.getValue().entrySet().stream().anyMatch(e-> "M".equals(e.getValue().getGender())))
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 13 hours ago









            Tordek

            6,96622861




            6,96622861























                0














                Seems like what you're after is given a Entry<String,Map<String,Employee>> if there's any employee who has a gender of "M" then filter the inner Map<String,Employee> to contain only entries with a gender "M".



                in which case you can filter along with anyMatch for the first criterion. Further, at the collecting phase, you can then apply the filtering on the inner map:



                tempCollection.entrySet().stream()
                .filter(i -> i.getValue().values().stream().anyMatch(e -> "M".equals(e.getGender())))
                .collect(toMap(Map.Entry::getKey,
                v -> v.getValue().entrySet().stream()
                .filter(i -> "M".equals(i.getValue().getGender()))
                .collect(toMap(Map.Entry::getKey, Map.Entry::getValue))));





                share|improve this answer























                • anyMatch returns all the values in the map even there is only one employee with Gender M.
                  – user1578872
                  12 hours ago










                • @user1578872 right, thanks to the accepted answer, I can now understand your requirement. the description wasn't very clear IMO. anyway edited to accommodate.
                  – Aomine
                  12 hours ago


















                0














                Seems like what you're after is given a Entry<String,Map<String,Employee>> if there's any employee who has a gender of "M" then filter the inner Map<String,Employee> to contain only entries with a gender "M".



                in which case you can filter along with anyMatch for the first criterion. Further, at the collecting phase, you can then apply the filtering on the inner map:



                tempCollection.entrySet().stream()
                .filter(i -> i.getValue().values().stream().anyMatch(e -> "M".equals(e.getGender())))
                .collect(toMap(Map.Entry::getKey,
                v -> v.getValue().entrySet().stream()
                .filter(i -> "M".equals(i.getValue().getGender()))
                .collect(toMap(Map.Entry::getKey, Map.Entry::getValue))));





                share|improve this answer























                • anyMatch returns all the values in the map even there is only one employee with Gender M.
                  – user1578872
                  12 hours ago










                • @user1578872 right, thanks to the accepted answer, I can now understand your requirement. the description wasn't very clear IMO. anyway edited to accommodate.
                  – Aomine
                  12 hours ago
















                0












                0








                0






                Seems like what you're after is given a Entry<String,Map<String,Employee>> if there's any employee who has a gender of "M" then filter the inner Map<String,Employee> to contain only entries with a gender "M".



                in which case you can filter along with anyMatch for the first criterion. Further, at the collecting phase, you can then apply the filtering on the inner map:



                tempCollection.entrySet().stream()
                .filter(i -> i.getValue().values().stream().anyMatch(e -> "M".equals(e.getGender())))
                .collect(toMap(Map.Entry::getKey,
                v -> v.getValue().entrySet().stream()
                .filter(i -> "M".equals(i.getValue().getGender()))
                .collect(toMap(Map.Entry::getKey, Map.Entry::getValue))));





                share|improve this answer














                Seems like what you're after is given a Entry<String,Map<String,Employee>> if there's any employee who has a gender of "M" then filter the inner Map<String,Employee> to contain only entries with a gender "M".



                in which case you can filter along with anyMatch for the first criterion. Further, at the collecting phase, you can then apply the filtering on the inner map:



                tempCollection.entrySet().stream()
                .filter(i -> i.getValue().values().stream().anyMatch(e -> "M".equals(e.getGender())))
                .collect(toMap(Map.Entry::getKey,
                v -> v.getValue().entrySet().stream()
                .filter(i -> "M".equals(i.getValue().getGender()))
                .collect(toMap(Map.Entry::getKey, Map.Entry::getValue))));






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 12 hours ago

























                answered 13 hours ago









                Aomine

                38.7k73365




                38.7k73365












                • anyMatch returns all the values in the map even there is only one employee with Gender M.
                  – user1578872
                  12 hours ago










                • @user1578872 right, thanks to the accepted answer, I can now understand your requirement. the description wasn't very clear IMO. anyway edited to accommodate.
                  – Aomine
                  12 hours ago




















                • anyMatch returns all the values in the map even there is only one employee with Gender M.
                  – user1578872
                  12 hours ago










                • @user1578872 right, thanks to the accepted answer, I can now understand your requirement. the description wasn't very clear IMO. anyway edited to accommodate.
                  – Aomine
                  12 hours ago


















                anyMatch returns all the values in the map even there is only one employee with Gender M.
                – user1578872
                12 hours ago




                anyMatch returns all the values in the map even there is only one employee with Gender M.
                – user1578872
                12 hours ago












                @user1578872 right, thanks to the accepted answer, I can now understand your requirement. the description wasn't very clear IMO. anyway edited to accommodate.
                – Aomine
                12 hours ago






                @user1578872 right, thanks to the accepted answer, I can now understand your requirement. the description wasn't very clear IMO. anyway edited to accommodate.
                – Aomine
                12 hours ago













                0














                Other way would be like this:



                map.values()
                .removeIf(entry->entry.values().stream().anyMatch(e -> !"M".equals(e.getGender())));

                map.entrySet()
                .removeIf(entry->entry.getValue().size() == 0);





                share|improve this answer


























                  0














                  Other way would be like this:



                  map.values()
                  .removeIf(entry->entry.values().stream().anyMatch(e -> !"M".equals(e.getGender())));

                  map.entrySet()
                  .removeIf(entry->entry.getValue().size() == 0);





                  share|improve this answer
























                    0












                    0








                    0






                    Other way would be like this:



                    map.values()
                    .removeIf(entry->entry.values().stream().anyMatch(e -> !"M".equals(e.getGender())));

                    map.entrySet()
                    .removeIf(entry->entry.getValue().size() == 0);





                    share|improve this answer












                    Other way would be like this:



                    map.values()
                    .removeIf(entry->entry.values().stream().anyMatch(e -> !"M".equals(e.getGender())));

                    map.entrySet()
                    .removeIf(entry->entry.getValue().size() == 0);






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 11 hours ago









                    Hadi J

                    9,70731641




                    9,70731641























                        0














                        You may do it like so,



                        Map<String, Map<String, Employee>> maleEmpMap = tempCollection.entrySet().stream()
                        .map(e -> new AbstractMap.SimpleEntry<>(e.getKey(),
                        e.getValue().entrySet().stream().filter(emp -> "M".equals(emp.getValue().getGender()))
                        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))))
                        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));


                        Take each map entry from the outer map and create a new map entry using the same key and the newly constructed nested map with the same key and which has only male employees as it's value. Finally collect all those matching entries into a new outer map. This will gracefully handle the empty scenario which you mentioned in the problem statement above.






                        share|improve this answer




























                          0














                          You may do it like so,



                          Map<String, Map<String, Employee>> maleEmpMap = tempCollection.entrySet().stream()
                          .map(e -> new AbstractMap.SimpleEntry<>(e.getKey(),
                          e.getValue().entrySet().stream().filter(emp -> "M".equals(emp.getValue().getGender()))
                          .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))))
                          .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));


                          Take each map entry from the outer map and create a new map entry using the same key and the newly constructed nested map with the same key and which has only male employees as it's value. Finally collect all those matching entries into a new outer map. This will gracefully handle the empty scenario which you mentioned in the problem statement above.






                          share|improve this answer


























                            0












                            0








                            0






                            You may do it like so,



                            Map<String, Map<String, Employee>> maleEmpMap = tempCollection.entrySet().stream()
                            .map(e -> new AbstractMap.SimpleEntry<>(e.getKey(),
                            e.getValue().entrySet().stream().filter(emp -> "M".equals(emp.getValue().getGender()))
                            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))))
                            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));


                            Take each map entry from the outer map and create a new map entry using the same key and the newly constructed nested map with the same key and which has only male employees as it's value. Finally collect all those matching entries into a new outer map. This will gracefully handle the empty scenario which you mentioned in the problem statement above.






                            share|improve this answer














                            You may do it like so,



                            Map<String, Map<String, Employee>> maleEmpMap = tempCollection.entrySet().stream()
                            .map(e -> new AbstractMap.SimpleEntry<>(e.getKey(),
                            e.getValue().entrySet().stream().filter(emp -> "M".equals(emp.getValue().getGender()))
                            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))))
                            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));


                            Take each map entry from the outer map and create a new map entry using the same key and the newly constructed nested map with the same key and which has only male employees as it's value. Finally collect all those matching entries into a new outer map. This will gracefully handle the empty scenario which you mentioned in the problem statement above.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited 11 hours ago

























                            answered 11 hours ago









                            Ravindra Ranwala

                            8,30731634




                            8,30731634






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53927656%2fjava-8-strem-filter-map-in-map-mapstring-mapstring-employee%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                What visual should I use to simply compare current year value vs last year in Power BI desktop

                                How to ignore python UserWarning in pytest?

                                Alexandru Averescu