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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












5














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
    Dec 26 '18 at 5:22















5














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
    Dec 26 '18 at 5:22













5












5








5


2





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 Dec 26 '18 at 5:49









Aomine

41k74071




41k74071










asked Dec 26 '18 at 5:09









user1578872user1578872

1,58452565




1,58452565











  • Return all the employees whose gender is M.
    – user1578872
    Dec 26 '18 at 5:22
















  • Return all the employees whose gender is M.
    – user1578872
    Dec 26 '18 at 5:22















Return all the employees whose gender is M.
– user1578872
Dec 26 '18 at 5:22




Return all the employees whose gender is M.
– user1578872
Dec 26 '18 at 5:22












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
    Dec 26 '18 at 5:25










  • If none of the employee is with the Gender "M", it still returns Map<String,emptyMap()>
    – user1578872
    Dec 26 '18 at 5:31










  • @user1578872 i have to return empty map if the filtered result is empty... that's what you meant, right?
    – nullpointer
    Dec 26 '18 at 5:32











  • It should be completeEmpty map in this case. Like, return emptyMap(), not Map<String,emptyMap()>
    – user1578872
    Dec 26 '18 at 5:33






  • 2




    I think put would be more ideal rather than putIfAbsent as the key will always be unique.
    – Aomine
    Dec 26 '18 at 6:19



















1














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




























    1














    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
      Dec 26 '18 at 5:44










    • @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
      Dec 26 '18 at 6:00



















    1














    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














      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




















        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
          Dec 26 '18 at 5:25










        • If none of the employee is with the Gender "M", it still returns Map<String,emptyMap()>
          – user1578872
          Dec 26 '18 at 5:31










        • @user1578872 i have to return empty map if the filtered result is empty... that's what you meant, right?
          – nullpointer
          Dec 26 '18 at 5:32











        • It should be completeEmpty map in this case. Like, return emptyMap(), not Map<String,emptyMap()>
          – user1578872
          Dec 26 '18 at 5:33






        • 2




          I think put would be more ideal rather than putIfAbsent as the key will always be unique.
          – Aomine
          Dec 26 '18 at 6:19
















        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
          Dec 26 '18 at 5:25










        • If none of the employee is with the Gender "M", it still returns Map<String,emptyMap()>
          – user1578872
          Dec 26 '18 at 5:31










        • @user1578872 i have to return empty map if the filtered result is empty... that's what you meant, right?
          – nullpointer
          Dec 26 '18 at 5:32











        • It should be completeEmpty map in this case. Like, return emptyMap(), not Map<String,emptyMap()>
          – user1578872
          Dec 26 '18 at 5:33






        • 2




          I think put would be more ideal rather than putIfAbsent as the key will always be unique.
          – Aomine
          Dec 26 '18 at 6:19














        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 Dec 26 '18 at 16:29

























        answered Dec 26 '18 at 5:13









        nullpointernullpointer

        43.8k1095182




        43.8k1095182











        • 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
          Dec 26 '18 at 5:25










        • If none of the employee is with the Gender "M", it still returns Map<String,emptyMap()>
          – user1578872
          Dec 26 '18 at 5:31










        • @user1578872 i have to return empty map if the filtered result is empty... that's what you meant, right?
          – nullpointer
          Dec 26 '18 at 5:32











        • It should be completeEmpty map in this case. Like, return emptyMap(), not Map<String,emptyMap()>
          – user1578872
          Dec 26 '18 at 5:33






        • 2




          I think put would be more ideal rather than putIfAbsent as the key will always be unique.
          – Aomine
          Dec 26 '18 at 6:19

















        • 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
          Dec 26 '18 at 5:25










        • If none of the employee is with the Gender "M", it still returns Map<String,emptyMap()>
          – user1578872
          Dec 26 '18 at 5:31










        • @user1578872 i have to return empty map if the filtered result is empty... that's what you meant, right?
          – nullpointer
          Dec 26 '18 at 5:32











        • It should be completeEmpty map in this case. Like, return emptyMap(), not Map<String,emptyMap()>
          – user1578872
          Dec 26 '18 at 5:33






        • 2




          I think put would be more ideal rather than putIfAbsent as the key will always be unique.
          – Aomine
          Dec 26 '18 at 6:19
















        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
        Dec 26 '18 at 5:25




        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
        Dec 26 '18 at 5:25












        If none of the employee is with the Gender "M", it still returns Map<String,emptyMap()>
        – user1578872
        Dec 26 '18 at 5:31




        If none of the employee is with the Gender "M", it still returns Map<String,emptyMap()>
        – user1578872
        Dec 26 '18 at 5:31












        @user1578872 i have to return empty map if the filtered result is empty... that's what you meant, right?
        – nullpointer
        Dec 26 '18 at 5:32





        @user1578872 i have to return empty map if the filtered result is empty... that's what you meant, right?
        – nullpointer
        Dec 26 '18 at 5:32













        It should be completeEmpty map in this case. Like, return emptyMap(), not Map<String,emptyMap()>
        – user1578872
        Dec 26 '18 at 5:33




        It should be completeEmpty map in this case. Like, return emptyMap(), not Map<String,emptyMap()>
        – user1578872
        Dec 26 '18 at 5:33




        2




        2




        I think put would be more ideal rather than putIfAbsent as the key will always be unique.
        – Aomine
        Dec 26 '18 at 6:19





        I think put would be more ideal rather than putIfAbsent as the key will always be unique.
        – Aomine
        Dec 26 '18 at 6:19














        1














        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

























          1














          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























            1












            1








            1






            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 Dec 26 '18 at 5:17









            TordekTordek

            6,98622861




            6,98622861





















                1














                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
                  Dec 26 '18 at 5:44










                • @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
                  Dec 26 '18 at 6:00
















                1














                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
                  Dec 26 '18 at 5:44










                • @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
                  Dec 26 '18 at 6:00














                1












                1








                1






                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 Dec 26 '18 at 6:11

























                answered Dec 26 '18 at 5:16









                AomineAomine

                41k74071




                41k74071











                • anyMatch returns all the values in the map even there is only one employee with Gender M.
                  – user1578872
                  Dec 26 '18 at 5:44










                • @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
                  Dec 26 '18 at 6:00

















                • anyMatch returns all the values in the map even there is only one employee with Gender M.
                  – user1578872
                  Dec 26 '18 at 5:44










                • @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
                  Dec 26 '18 at 6:00
















                anyMatch returns all the values in the map even there is only one employee with Gender M.
                – user1578872
                Dec 26 '18 at 5:44




                anyMatch returns all the values in the map even there is only one employee with Gender M.
                – user1578872
                Dec 26 '18 at 5:44












                @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
                Dec 26 '18 at 6:00





                @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
                Dec 26 '18 at 6:00












                1














                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



























                  1














                  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

























                    1












                    1








                    1






                    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 Dec 26 '18 at 7:03

























                    answered Dec 26 '18 at 6:57









                    Ravindra RanwalaRavindra Ranwala

                    8,56031634




                    8,56031634





















                        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 Dec 26 '18 at 6:58









                            Hadi JHadi J

                            9,95731743




                            9,95731743



























                                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

                                How to check contact read email or not when send email to Individual?

                                Bahrain

                                Postfix configuration issue with fips on centos 7; mailgun relay