Java 8 Filter Map<String,List<Employee>>

夙愿已清 提交于 2019-12-10 13:58:06

问题


How to filter a Map<String, List<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,List<Employee>>
Output: Map<String,List<Employee>>
Filter criteria: 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


回答1:


To filter out entries where a list contains an employee who is not of the "M" gender:

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:

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.

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:

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



回答2:


In Java 8 you can convert a Map.entrySet() into a stream, follow by a filter() and collect() it. 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.

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



回答3:


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:


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.




回答5:


If you want to filter the map entries if any Employee in the list of the entry has gender = M, use the following code:

    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:

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:


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



回答7:


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



回答8:


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


来源:https://stackoverflow.com/questions/51629897/java-8-filter-mapstring-listemployee

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!