Reputation: 2578
Here is the answer of this question but I need is there any other way
Let Suppose Person is a class contains attribute
An ArrayList hold thousand person object ,I want to check that "11" personId is in ArayList or not?
One way is to iterate(Loop) arraylist and check single by single.
Is there any other way to resolve this?
Upvotes: 3
Views: 2006
Reputation: 1190
Override equals() and hashcode() method in your POJO for person Id
eg:
import java.util.ArrayList;
public class Test {
private int personId;
private String name;
//getters and Setters
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + personId;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Test other = (Test) obj;
if (personId != other.personId)
return false;
return true;
}
public static void main(String[] args) {
ArrayList<Test> test=new ArrayList<Test>();
Test t=new Test();
t.setName("Sireesh");
t.setPersonId(1);
Test t1=new Test();
t1.setName("Ramesh");
t1.setPersonId(2);
Test t2=new Test();
t2.setName("Rajesh");
t2.setPersonId(3);
test.add(t);
test.add(t1);
test.add(t2);
Test tx=new Test();
tx.setPersonId(1);
System.out.println(test.contains(tx));
//Returns true
}
}
Upvotes: 5
Reputation: 5258
Implement equals
and hashCode
based on persionId
. java.util.ArrayList#contains
will give you results. This solution is as good as looping through list and finding the object.
Upvotes: 1