简体   繁体   中英

Hashmap filtering in java lambda

Want a functional style filtering on two hashmap...

Say... I got a hashmap data as given map from database

    Map<String, Object> user=new HashMap<>();
    user.put("Name", "SomeName");
    user.put("Role", userRoles);
    //some relevant keys and value

The userRoles is a list of strings like below..

    List<String> userRoles = new ArrayList<>();
    userRoles.add("User");
    userRoles.add("Admin");

And the service permission from db

    List<String> servicePermission = new ArrayList<>();
    servicePermission.add("User");
    servicePermission.add("Public");

Now I want to filter with lambda... if any of userRoles exists in servicePermission list return true or return false/exception.

Thanks in advance

You can do the following.

return ((List<String>) user.get("Role")).stream()
                           .anyMatch(servicePermisions::contains);

You can use anyMatch :

boolean match = 
    user.get("Role").stream()
                    .anyMatch(s -> servicePermission.contains(s));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM