I want to check whether a List
contains an object that has a field with a certain value. Now, I could use a loop to go through and check, but I was curious if there was anything more code efficient.
Something like;
if(list.contains(new Object().setName("John"))){
//Do some stuff
}
I know the above code doesn't do anything, it's just to demonstrate roughly what I am trying to achieve.
Also, just to clarify, the reason I don't want to use a simple loop is because this code will currently go inside a loop that is inside a loop which is inside a loop. For readability I don't want to keep adding loops to these loops. So I wondered if there were any simple(ish) alternatives.
contains
method usesequals
internally. So you need to override theequals
method for your class as per your need.Btw this does not look syntatically correct:
You have two choices.
1. The first choice, which is preferable, is to override the `equals()` method in your Object class.
Let's say, for example, you have this Object class:
Now let's say you only care about the MyObject's name, that it should be unique so if two `MyObject`s have the same name they should be considered equal. In that case, you would want to override the `equals()` method (and also the `hashcode()` method) so that it compares the names to determine equality.
Once you've done this, you can check to see if a Collection contains a MyObject with the name "foo" by like so:
However, this might not be an option for you if:
If either of these are the case, you'll want option 2:
2. Write your own utility method:
Alternatively, you could extend ArrayList (or some other collection) and then add your own method to it:
Unfortunately there's not a better way around it.
Predicate
If you dont use Java 8, or library which gives you more functionality for dealing with collections, you could implement something which can be more reusable than your solution.
i'm using something like that, i have predicate interface, and i'm passing it implementation to my util class.
What is advantage of doing this in my way? you have one method which deals with searching in any type collection. and you dont have to create separate methods if you want to search by different field. alll what you need to do is provide different predicate which can be destroyed as soon as it no longer usefull/
if you want to use it, all what you need to do is call method and define tyour predicate
Here is a solution using Guava
Collection.contains()
is implemented by callingequals()
on each object until one returnstrue
.So one way to implement this is to override
equals()
but of course, you can only have one equals.Frameworks like Guava therefore use predicates for this. With
Iterables.find(list, predicate)
, you can search for arbitrary fields by putting the test into the predicate.Other languages built on top of the VM have this built in. In Groovy, for example, you simply write:
Java 8 made all our lives easier, too:
If you care about things like this, I suggest the book "Beyond Java". It contains many examples for the numerous shortcomings of Java and how other languages do better.
Map
You could create a
Hashmap<String, Object>
using one of the values as a key, and then seeing ifyourHashMap.keySet().contains(yourValue)
returns true.