Java 8 Strem filter map in map — Map<String,Map>
Clash Royale CLAN TAG#URR8PPP
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
add a comment |
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
Return all the employees whose gender is M.
– user1578872
Dec 26 '18 at 5:22
add a comment |
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
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
java java-8 hashmap java-stream
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
add a comment |
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
add a comment |
5 Answers
5
active
oldest
votes
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)));
);
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 thinkput
would be more ideal rather thanputIfAbsent
as thekey
will always be unique.
– Aomine
Dec 26 '18 at 6:19
|
show 3 more comments
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));
add a comment |
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))));
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
add a comment |
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.
add a comment |
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);
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)));
);
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 thinkput
would be more ideal rather thanputIfAbsent
as thekey
will always be unique.
– Aomine
Dec 26 '18 at 6:19
|
show 3 more comments
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)));
);
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 thinkput
would be more ideal rather thanputIfAbsent
as thekey
will always be unique.
– Aomine
Dec 26 '18 at 6:19
|
show 3 more comments
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)));
);
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)));
);
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 thinkput
would be more ideal rather thanputIfAbsent
as thekey
will always be unique.
– Aomine
Dec 26 '18 at 6:19
|
show 3 more comments
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 thinkput
would be more ideal rather thanputIfAbsent
as thekey
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
|
show 3 more comments
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));
add a comment |
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));
add a comment |
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));
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));
answered Dec 26 '18 at 5:17
TordekTordek
6,98622861
6,98622861
add a comment |
add a comment |
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))));
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
add a comment |
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))));
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
add a comment |
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))));
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))));
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Dec 26 '18 at 7:03
answered Dec 26 '18 at 6:57
Ravindra RanwalaRavindra Ranwala
8,56031634
8,56031634
add a comment |
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered Dec 26 '18 at 6:58
Hadi JHadi J
9,95731743
9,95731743
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Return all the employees whose gender is M.
– user1578872
Dec 26 '18 at 5:22