简体   繁体   中英

Filter Map based on condition in Java

I have a Map object

public class Employee {
    private int id;
    private String name;
    private String designation; 
}

Map<Integer, Employee> map = new HashMap<Integer, Employee>();

I want to filter the map based on one property in Employee which should match user input given property, say userName.

I am using

public List<Employee> getEmployeesByName(String userName) {
    map.entrySet().stream().filter(e-> e.getValue().getName().equals(userName)).collect(Collectors.toList());
}

I want the output in List<Employee> format. I don't understand why is it in format of List<Entry<Integer, Employee>> . What am I missing here?

You appear to be streaming Map.Entry s but just want the values. Change map.stream() (shouldn't work) to map.values().stream() .

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