• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method for searching for object in ArrayList  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I keep thinking there must be thousands of examples of this on the web, but I'm not finding it.

I'm working with an array of objects that contain an alphanumeric code number, name, and other information.

After building an ArrayList of the objects, and then sorting them using Collections.sort to sort the array based on the code number, I'm trying to figure out how to search the array to find an object using code numbers.

I assume the final solution will need a version of the CustomComparator created for Collections.sort, but I'm not seeing how to set up the search call.
Any tips are greatly appreciated.

Thanks in advance.
 
Ranch Hand
Posts: 98
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi and welcome to the Ranch!

So to be clear on your issue:

You wish to search through an ArrayList, until a given object in the list equals the specified code number?

I'm sure you could find something in the javadoc for ArrayList that would meet your needs.
 
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found out a method in Collection class which you will find here in docs. Does it helps?
 
Jeffrey Schmitt
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. The "trick" turned out to be that instead of just searching directly on the code value, I had to create a new object and write the code value into that, then use it for the search.

SIObj SIkey = new SIObj(); // new SI object to use to search
SIkey.strCode = SIcode; // write the key to search for into the new object
indexSI = Collections.binarySearch(SIObjs, SIkey, new CustomComparator());


public class CustomComparator implements Comparator<SIObj> {

public int compare(SIObj o1, SIObj o2) {
int result = o1.strCode.compareTo(o2.strCode);
return (result<0 ? -1 : (result == 0 ? 0 : 1));
}
}


Seems a bit round-about, so I sort of feel like just using my own binary search was better, but maybe the Collections search is optimized beyond what a Java programmer could do.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeffrey Schmitt wrote:Seems a bit round-about


Not really. It's just a very general solution. You can search for any type of object, using whatever sorting and equality rules you want. So you have to provide the information necessary to determine a match--an object with the appropraite fields set.

so I sort of feel like just using my own binary search was better


Maybe it was, for your particular requirements for this particular use case.

but maybe the Collections search is optimized beyond what a Java programmer could do.


Nope. It's just normal Java code written in exactly the same way than any other Java programmer could write code. You or I could very easily implement that method the same way as what's in the API if it weren't there.
 
Jeffrey Schmitt
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff. I agree with your comments. The part that seems a bit roundabout to me is having to create a new object to run the search. It would seem more straightforward to pass the key value being searched for and then setting up the comparator to use that key and the appropriate object element. I'm thinking there probably is a way to do that I just haven't learned it yet. Thanks again.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!