Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Custom Search With ArrayList

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Employee Class with two attributes:- empid and empname.

I have around 1000 Employee Class objects stored in an ArrayList. Now I need to search an Employee Objectin the arraylist based on the Employee ID.

Currently, I am iterating on the arrayList and checking each object's empid property.

Can I write method similar to the contains() of the ArrayList.

Can anyone provide some inputs on this.
 
Marshal
Posts: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

What format is the ID field in? Is it immutable? Can you use it as the "key" in a Map?
 
Sheriff
Posts: 21974
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"));
}
 
Rob Spoor
Sheriff
Posts: 21974
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should find some links about the equals method here.
 
Rob Spoor
Sheriff
Posts: 21974
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Override equals properly, then create a new Employee instance to look for:
This will only work if equals uses only the employee code, not the name.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

And please UseCodeTags when you post source code.
 
    Bookmark Topic Watch Topic
  • New Topic