简体   繁体   English

Java 8过滤器映射 <String,List<Employee> &gt;

[英]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.

相关问题 Java 8 Strem过滤地图在地图中 - Map <String,Map <String,Employee >> - Java 8 Strem filter map in map — Map<String,Map<String,Employee>> 如何转换列表<Employee>到地图<Employee, List<String> &gt; 使用 java8? - How do I convert a List<Employee> to Map<Employee, List<String>> using java8? Java 8,列表 <Employee> 映射部门和地址列表 - java 8,List<Employee> to map of department and list of addresses 用于 Map 的 Java 8 流过滤器<String, List<Object> &gt; - Java 8 stream filter for Map<String, List<Object>> Java 8 过滤器和列表收集<Map<String, Object> &gt; - Java 8 Filter and Collect of List<Map<String, Object>> 转换 Map <string,list<person> &gt; 至 Map <string,list<employee> &gt;,使用 Java 8 个流。 我这样做了,但是没有 for 循环怎么做</string,list<employee></string,list<person> - Convert Map<String,List<Person>> to Map<String,List<Employee>>, using Java 8 streams. I did this but how do this without for loop 过滤器 Map <string,list<object> &gt; 使用 Java 流</string,list<object> - Filter Map<String,List<Object>> Using Java Streams Java Stream 比较和过滤来自 Map 的条目<string, map<string, list<string> &gt;&gt;</string,> - Java Stream compare and filter entries from Map<String, Map<String, List<String>>> 如何根据“城市过滤器列表”过滤员工列表并使用 java 8 维护过滤器顺序 - How can I filter employee list based on the "City filter list" and maintain the filter order using java 8 将员工数据存储在java中的地图中 - storing the employee data in a map in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM