If the equals method only checks the ID then you can create a new Employee instance with the same ID and then use contains. This will still loop through the list though, only it's the contains method that does it, not your code.
I agree with Campbell; a Map with the IDs as keys and Employee instances as values looks like a better candidate. You can use containsKey, get etc for lookups, and values() to get a Collection<Employee> to iterate over the employees directly.
If you only need to see if an Employee is present in your collection, you don't need to overwrite contains(), just rely on its behaviour: overwrite equals() and hashCode() in your Employee class and contains() will return correctly. Also, in this scenario, you don't really need a List; a Set -- HashSet, for instance, would be more suitable.
But if you need to actually refer to that Employee instance, you would definitely need a Map<ID, Employee> -- a HashMap or (if you want the order to be preserved) a LinkedHashMap. Make sure your ID type overwrites equals() and hashCode() and that it's immutable (as Campbell pointed out).
Sasanka Boxi
Greenhorn
Joined: Jan 04, 2011
Posts: 6
posted
0
As you suggested I have tried to override the equals and hashcode method....No Idea how to override the equals method properly....
But Still its not working for me....I have pasted some sample code here
public class Risking
{
String empcode;
String name;
public String getEmpcode() {
return empcode;
}
public void setEmpcode(String empcode) {
this.empcode = empcode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Risking(String empcode, String name)
{
this.empcode = empcode;
this.name = name;
}
public boolean equals(Object obj)
{
if(obj==null)
return false;
String s= (String)obj;
return s.equals(empcode);
}
public int hashCode()
{
final int PRIME = 31;
int result = 1;
result = PRIME * result + empcode.hashCode();
return result;
}
public static void main(String args[])
{
ArrayList aList = new ArrayList();
Risking r1 = new Risking("117","Rocky");
Risking r2 = new Risking("118","Vicky");
aList.add(r1);
aList.add(r2);
System.out.println(aList.contains("117"));
}
1) Please UseCodeTags next time
2) Your equals method is so very very wrong... Look at the contract for Object.equals, and you'll see that your implementation violates several of the required points.
Sasanka Boxi
Greenhorn
Joined: Jan 04, 2011
Posts: 6
posted
0
Hello Rob,
We can Override the equals method to compare two similar object(not sure???)
But in my case its the comparision between the object and the String. Can you please place some hints how should I write the code to
override equals() and get the result.
Thanking You.
Sasanka
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
4
posted
0
You should find some links about the equals method here.
Here is a hint about what is wrong with your equals() method:
You must not treat the argument that is passed in as a String object, and compare it to empcode, as you are doing. Instead, you must treat it as a Risking object, and compare its empcode with the empcode of the current object.