[英]Java 8 Filter Map<String,List<Employee>>
How to filter a Map<String, List<Employee>>
using Java 8 Filter? 如何使用Java 8 Filter过滤
Map<String, List<Employee>>
?
I have to filter only when any of employee in the list having a field value Gender = "M"
. 我必须仅在列表中具有字段值
Gender = "M"
任何员工时进行过滤。
Input: Map<String,List<Employee>>
输入:
Map<String,List<Employee>>
Output: Map<String,List<Employee>>
输出:
Map<String,List<Employee>>
Filter criteria: Employee.genter = "M"
筛选条件:
Employee.genter = "M"
Also i have to filter out the key in the output map (or filter map [new map we get after filter]) if the List<> is empty on the map value 另外我必须过滤掉输出图中的键(或过滤器图[过滤后我们得到的新地图])如果List <>在地图上为空
To filter out entries where a list contains an employee who is not of the "M"
gender: 过滤掉列表中包含不属于
"M"
性别的员工的条目 :
Map<String, List<Employee>> r2 = map.entrySet().stream()
.filter(i -> i.getValue().stream().allMatch(e-> "M".equals(e.gender)))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
To filter out employees who are not of the "M"
gender: 筛选出不属于
"M"
性别的员工 :
Map<String, List<Employee>> r1 = map.entrySet().stream()
.filter(i -> !i.getValue().isEmpty())
.collect(Collectors.toMap(Map.Entry::getKey,
i -> i.getValue().stream()
.filter(e -> "M".equals(e.gender)).collect(Collectors.toList())));
To filter out entries where a list doesn't contain any "M"
employee. 过滤掉列表中不包含任何
"M"
员工的条目 。
Map<String, List<Employee>> r3 = map.entrySet().stream()
.filter(i -> i.getValue().stream().anyMatch(e -> "M".equals(e.gender)))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
Let's have 2 entries in the map: 我们在地图上有2个条目:
"1" -> ["M", "M", "M"]
"2" -> ["M", "F", "M"]
The results for them will be: 他们的结果将是:
r1 = {1=[M, M, M], 2=[M, M]}
r2 = {1=[M, M, M]}
r3 = {1=[M, M, M], 2=[M, F, M]}
In Java 8 you can convert a Map.entrySet()
into a stream, follow by a filter()
and collect()
it. 在Java 8中,您可以将
Map.entrySet()
转换为流,然后使用filter()
和collect()
。 Example taken from here . 从这里取的例子。
Map<Integer, String> map = new HashMap<>();
map.put(1, "linode.com");
map.put(2, "heroku.com");
//Map -> Stream -> Filter -> String
String result = map.entrySet().stream()
.filter(x -> "something".equals(x.getValue()))
.map(x->x.getValue())
.collect(Collectors.joining());
//Map -> Stream -> Filter -> MAP
Map<Integer, String> collect = map.entrySet().stream()
.filter(x -> x.getKey() == 2)
.collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
// or like this
Map<Integer, String> collect = map.entrySet().stream()
.filter(x -> x.getKey() == 3)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
And for your case it would look like this, because you also need to find out if there is a match in a List
of object of class Employee
. 对于你的情况,它看起来像这样,因为你还需要找出类
Employee
的对象List
中是否匹配。
Map<String, List<Employee>> collect = map.entrySet().stream()
.filter(x -> x.getValue().stream()
.anyMatch(employee -> employee.Gender.equals("M")))
.collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
Filter only map entries that have only male Employes: 仅过滤仅包含男性员工的地图条目:
@Test
public void filterOnlyMales(){
String gender = "M";
Map<String, List<Employee>> maleEmployees = map.entrySet()
.stream()
/*Filter only keys with male Employes*/
.filter(entry -> entry.getValue().stream()
.anyMatch(empl -> gender.equals(empl.getGender())))
.collect(Collectors.toMap(
Map.Entry::getKey,
p -> filterMalesOnly(gender, p));
}
private List<Employee> filterMalesOnly(String gender,
Map.Entry<String, List<Employee>> p) {
return p.getValue()
.stream()
.filter(empl -> gender.equals(empl.getGender()))
.collect(
Collectors.toList());
}
Map<String, List<Employee>> result = yourMap.entrySet()
.stream()
.flatMap(ent -> ent.getValue().stream().map(emp -> new SimpleEntry<>(ent.getKey(), emp)))
.filter(ent -> "M".equalsIgnoreCase(ent.getValue().getGender()))
.collect(Collectors.groupingBy(
Entry::getKey,
Collectors.mapping(Entry::getValue, Collectors.toList())));
You may do it like so, 你可以这样做,
Map<String, List<Employee>> resultMap = input.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey,
e -> e.getValue().stream().filter(emp -> emp.getGender().equals("M")).collect(Collectors.toList())));
You can use Collectors.toMap
with the same key, and derive the new value using the existing one. 您可以将
Collectors.toMap
与相同的键一起使用,并使用现有值导出新值。
If you want to filter the map entries if any Employee in the list of the entry has gender = M
, use the following code: 如果要在条目列表中的任何Employee具有gender =
M
过滤映射条目,请使用以下代码:
Map<String,List<Employee>> result = employeeMap.entrySet()
.stream()
.filter(e -> e.getValue()
.stream()
.anyMatch(employee -> employee.getGender().equalsIgnoreCase("M")))
.collect(Collectors.toMap(Entry::getKey,Entry::getValue));
And, If you want to filter out all the Employees with gender M
from each list, use the following code: 并且,如果要从每个列表中筛选出所有具有性别
M
的员工,请使用以下代码:
Map<String,List<Employee>> result = employeeMap.entrySet()
.stream()
.collect(Collectors.toMap(Entry::getKey,
e -> e.getValue().stream()
.filter(employee -> employee.getGender().equalsIgnoreCase("M"))
.collect(Collectors.toList())));
For instance: 例如:
Map<String, List<Employee>> result = originalMap.entrySet().stream()
.filter(es -> es.getValue().stream().anyMatch(emp -> emp.getGender().equals("M")))
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
Returning map of employee 返回员工的地图
public static Map<Integer, Employee> evaluatemapEmployee()
{
//return Dao.getselectedEmployee().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
//return Dao.getselectedEmployee().entrySet().stream().filter(emp->emp.getValue().getsalary()>8000).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
//return Dao.getselectedEmployee().entrySet().stream().filter(emp->emp.getValue().getEmpname().matches("om")).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
//return Dao.getselectedEmployee().entrySet().stream().filter(emp->emp.getValue().getEmpid()==103).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
return Dao.getselectedEmployee().entrySet().stream().filter(emp->emp.getValue().getEmpname().matches("kush")).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.