开发者

Get objects from List of objects based on variable in object

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..?


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


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.


I found a way in java8 with streams filter

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜