Reputation: 401
Assuming I have class like this:
class A {
int elementA;
int elementB
}
I also have an ArrayList
like this: ArrayList<A> listObj
.
How can I check if that list contains an object using only some of the properties of A
? E.g. considering only elementA
for finding if object newA{elementA}
is already in the list?
For object A
I have defined an equals
method, where I consider only the elementA
, however this is not enough.
Upvotes: 26
Views: 104528
Reputation: 1
List<Upload> mUploadPost = new ArrayList<Upload>();
Upload upload = new Upload();
upload.addName("Mpendulo");
upload.addLastName("Mtshali");
if(!mUploadPost.contains(upload)){
mUploadPost.add(upload);
}
Upvotes: 0
Reputation: 449
For comparing the object value in arrayList
.
Follow this Link.
How to compare Objects attributes in an ArrayList?
I hope this solution will help you. Thanks
Upvotes: 0
Reputation:
All the above solution will suits your question. What i like to suggest you here is !!! When ever you use collection, always have your variable datatype to its super class. which will help you to make more manipulation and convert to any of its sub-class.
Ex:
List<String> myList = new ArrayList<String>();
The advantage that the implementation of the List can change (to a LinkedList for example), without affecting the rest of the code. This is will be difficult to do with an ArrayList, not only because you will need to change ArrayList to LinkedList everywhere, but also because you may have used ArrayList specific methods.
Upvotes: 0
Reputation: 13821
You could adapt your equals
to accomodate for your needs, but you could also use one of the powerful collection libraries that already exists, like commons-collections, Guava or lambdaj. They all have iteration and predicate constructs that allow you to "explore" your collections based on some predicate.
Example with Commons Collections:
boolean contains = CollectionUtils.exists(myArrayList, new Predicate<A>() {
public boolean evaluate(A theA) {
// Do the comparison
}
});
Upvotes: 4
Reputation: 13177
I wouldn't override equals
for this. Even though {1, 1}
and {1, 2}
cannot both occur in your list, the two objects are not equal.
I would use a Map
instead:
Map<Integer, A> map = new HashMap<>();
A a1 = new A(1, 1);
A a2 = new A(1, 2);
map.put(a1.elementA, a1);
// test if key is contained
boolean contains = map.containsKey(a2.elementA);
// overwrite a1 with a2
map.put(a2.elementA, a2);
Upvotes: 1
Reputation: 49372
List#contains() method uses the equals()
method to evaluate if two objects are the same. So, you need to override equals()
in your Class A
and also override the hashCode()
.
@Override
public boolean equals(Object object)
{
boolean isEqual= false;
if (object != null && object instanceof A)
{
isEqual = (this.elementA == ((A) object).elementA);
}
return isEqual;
}
@Override
public int hashCode() {
return this.elementA;
}
Upvotes: 33
Reputation: 3263
The hashCode()
and equals()
of arrays are a bit broken when it comes to this (it is a long different discussion why).
A possible work around is to use ArrayList<ArrayList<String>>
instead of ArrayList<String[]>
, the equals() method for ArrayList will be as you expect it to.
For example:
ArrayList<String> l1 = new ArrayList<>();
ArrayList<String> l2 = new ArrayList<>();
l1.add("asdf");
l2.add("asdf");
ArrayList<ArrayList<String>> coll = new ArrayList<>();
coll.add(l1);
System.out.println(coll.contains(l2));
Will yield true, as expected
Also refer below link
Most efficient way to see if an ArrayList contains an object in Java
Upvotes: 0