Java 8筛选器映射<string,list <employee >>

时间:2018-08-01 09:33:37

标签: java java-8 java-stream filtering

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

仅当列表中的任何员工的字段值为Gender = "M"时,我才需要过滤。

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

如果List <>在地图值上为空,那么我还必须在输出地图(或过滤器地图[我们在过滤器后得到的新地图])中过滤出键

8 个答案:

答案 0 :(得分:2)

要过滤出条目,其中列表中包含一个非"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));

让我们在地图上有2个条目:

"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]}

答案 1 :(得分:1)

在Java 8中,您可以将Map.entrySet()转换为流,后跟filter()collect()。取自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));

对于您的情况,它看起来像这样,因为您还需要找出List类的对象的Employee中是否存在匹配项。

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

答案 2 :(得分:0)

仅过滤只有男性雇员的地图条目:

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

您可以这样做

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

您可以将Collectors.toMap与相同的密钥一起使用,并使用现有的密钥来导出新值。

答案 5 :(得分:0)

如果条目列表中的任何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())));

答案 6 :(得分:0)

例如:

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

答案 7 :(得分:0)

退还员工图

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