3

Here is the answer of this question but I need is there any other way

Let Suppose Person is a class contains attribute

  • personId
  • personName
  • personAddress

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?

Community
  • 1
  • 1
Yasir Shabbir Choudhary
  • 2,458
  • 2
  • 27
  • 31
  • 2
    Yes, create a second `Set` of `personId` to test against or simulate a database index by creating an additional `Map` of `personId` to `person`. – Smutje Feb 13 '15 at 11:14
  • You could build a `HashMap` where Integer is `Person.personId` and then use simple get: `get(id) == null`. – Everv0id Feb 13 '15 at 11:16
  • can you show me some sample but remember I am using Java and I need to only use ArrayList not HashMap or some other collections. – Yasir Shabbir Choudhary Feb 13 '15 at 11:17
  • If you use only an ArrayList there is no way around looping through it, either implicitely or explicitely. What are you trying to achieve? – Adrian Leonhard Feb 13 '15 at 11:19
  • I want to check 11 personId is in thousand person objects in arraylist or not ? I know there is contain() in arraylist but this compare whole person object but only presonId attribute. – Yasir Shabbir Choudhary Feb 13 '15 at 11:22

2 Answers2

5

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

}
}
Srinivasu
  • 1,215
  • 14
  • 32
Sireesh Vattikuti
  • 1,180
  • 1
  • 9
  • 21
  • 1
    @YasirShabbirChoudhary No, it isn't if you're using only `List`s. However, it is good practice to override both `hashCode()` and `equals()` in order to satisfy these operations contract, otherwise some Java collections wouldn't work as expected, i.e. if you used a `HashSet` you would run into trouble. See this question http://stackoverflow.com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java for more information. – fps Feb 13 '15 at 11:43
1

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.

Adisesha
  • 5,200
  • 1
  • 32
  • 43