Java 8 过滤器映射<字符串,列表<Employee>>



如何使用Java 8 filter过滤Map<String, List<Employee>>

只有当列表中的任何员工的字段值为Gender = "M"时,我才需要进行筛选。

输入:Map<String,List<Employee>>
输出:Map<String,List<Employee>>
过滤条件:Employee.genter = "M"

此外,如果List<>在地图值上为空

过滤列表中包含非"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));

筛选出"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())));

筛选列表中不包含任何"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));

让我们在地图中有两个条目:

"1" -> ["M", "M", "M"]
"2" -> ["M", "F", "M"]

他们的结果将是:

r1 = {1=[M, M, M], 2=[M, M]}
r2 = {1=[M, M, M]}
r3 = {1=[M, M, M], 2=[M, F, M]}

在Java 8中,您可以将Map.entrySet()转换为流,然后是filter()collect()。示例如下。

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));

对于您的情况,它看起来是这样的,因为您还需要找出类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()));
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())));

如果要过滤映射条目(如果条目列表中的任何Employee的性别为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));

而且,如果你想从每个列表中筛选出所有性别为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())));

只筛选只有男性员工的映射条目:

@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 = originalMap.entrySet().stream()
.filter(es -> es.getValue().stream().anyMatch(emp -> emp.getGender().equals("M")))
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));

返回员工地图

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));
}

最新更新