aspose file tools*
The moose likes Beginning Java and the fly likes Custom Search With ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Custom Search With ArrayList" Watch "Custom Search With ArrayList" New topic
Author

Custom Search With ArrayList

Sasanka Boxi
Greenhorn

Joined: Jan 04, 2011
Posts: 6
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39386
    
  28
Welcome to the Ranch

What format is the ID field in? Is it immutable? Can you use it as the "key" in a Map?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Costi Ciudatu
Ranch Hand

Joined: Oct 24, 2006
Posts: 74
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
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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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
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: 39386
    
  28
You should find some links about the equals method here.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Custom Search With ArrayList