Get objects from List of objects based on variable in object

杀马特。学长 韩版系。学妹 提交于 2019-12-09 16:35:58

问题


I have List of User object, I just want to get User objects from List based on variables in User object.

public class User {

    private int id;

    private String sex;

    private int age;

    private String country;

    /**
     * Getter and setter for all variables
     */
}

I have a model class like this. Now I have list of User objects.

List<User> users = new ArrayList<User>();

I want to get the objects from users list if the user is Male.

List<User> ageList = new ArrayList<User>();
for(User object : users) {
    if(object.getSex().equals("Male")){
        ageList.add(object);
    }
}

I do not like the above approach. Is there any better way to get objects from List of objects based on variable in object..?

Is there any functions Java Collections ..? can we solve this using Java Comparator..?


回答1:


If you're using Guava, you can use Collections2.filter:

Collection<User> males = Collections2.filter(users, new Predicate<User>() {
    @Override
    public boolean apply(User user) {
        return user.getSex().equals("Male");
    }
});

And with Java 8, you can do even better:

Collection<User> males = Collections2.filter(users, user -> user.getSex().equals("Male"));



回答2:


You can do this with lambda functions and reflections. However since Java doesn't support closures you will find that this approach is more error prone, requires more code and is slower.

If you want a fast way to do this you could main that a Map<Sec, List<User>> or MultiMap.

BTW: I would use an Enum for the Sex of the person. You should be able to limit yourself to a few possibilities ;) You could do similarly with Country.




回答3:


I found a way in java8 with streams filter

List<User> males = users.stream().filter(u -> u.getSex().equals("Male")).collect(Collectors.toList());



回答4:


For simple filters I like lambdaj onliners syntax: http://code.google.com/p/lambdaj/wiki/LambdajFeatures

List<User> ageList = filter(having(on(User.class).getSex(), equalTo("Male")), users);

For complex conditions it is better to create separate filter finction.




回答5:


Use MultiValueMap from Apache common collection or simply Map<String List<User>> to split users at the beginning, if get users according to sex is all what you need.

As long as you want to get particular users from an unsorted collection, search through whole collection is a must. So there is no better way to retrieve it. However, you can hide this logic beneath the surface.

Apache common collections (and Guava as Chris Jester-Young mentioned) provide CollectionUtils.select(), all you need to do is implement the Predicate. (check the answer from Chris Jester-Young for better explanation)

Or, instead of using collection to pass users around, make it into an object like Users or UserPack. Then you can implement a method call getMaleUsers() to return all male users inside the object for you. This trick is quite useful if you need to manipulate particular objects a lot.



来源:https://stackoverflow.com/questions/6242276/get-objects-from-list-of-objects-based-on-variable-in-object

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