Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Searching a list using a generic instance of a class

Posted on 2004-09-02
11
Medium Priority
?
266 Views
Last Modified: 2010-03-31
I'm trying to implement a list where I'll be storing specific instances of objects, but where I need to be able to find elements in the list based on traits they share in common with generic objects.

Basically, my stored classes have the attibutes "name" and "number".  I can have more than one of a given pairing of those, I just need the basic ability to find the *first* match in the list based on either those two pieces of data, ideally by passing a similar object to a search routine and having it find the first match.

Although it is possible to impose an ordering on the items, it's not really natural to do so, and there's a fair number of ways to cause it to mess up, since the items are grouped, but the groups don't have a natural ordering.
0
Comment
Question by:offline314159
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 11965218
Simply compare each object's value with the one on the List. Stop at the first one. For example if you have an object called Info with "name" and "number" attributes and a list "myList" you might as well do:

Info info toBeSearchedFor = new Info("girionis", 10);

for (int i=0; i<myList.size(); i++)
{
   Info info = (Info) myList.get(i);
   if (info.getName().equals(toBeSearchedFor.getName()) && info.getNumber() == toBeSearchedFor.getNumber())
   {
      // do whatever you want with the object.
      // you might as well return it, in this cae you do not need th ebreak statement below
      break;

   }
}
0
 

Author Comment

by:offline314159
ID: 11965280
I was hoping for something a bit less in the O(n) range for searching.  I was thinking of using a binary search, but I think that rests on the equals() method as well, and I don't want to override that for other reasons of my own.  Not to mention the fact that any ordering I impose on these items will be a bit of a kludge.

If all else fails, I guess I'll go linear on it, but that's just excessively inefficient from where I sit.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 11965353
binarySearch is exactly what you need:

Info infoToBeSearchedFor = new Info("girionis", 10);
Collections.binarySearcch(yourList, infoToBeSearchedFor, new Comparator() {
      public int compare(Object o1, Object o2) {
            Info info1 = (Info)o1;
            Info info2 = (Info)o2;
            return info1.getName().hashCode() + info1.getNumber() - info2.getName().hashCode() + info2.getNumber();
      }
});
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 11965365
Typo there:

>>Collections.binarySearcch

should obviously be

Collections.binarySearch

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11965402
Oh - and you'll need to use the return value from that method too ;-)
0
 
LVL 35

Expert Comment

by:girionis
ID: 11965431
>  I was thinking of using a binary search

Binary search will work on sorted items. I seem to remember you said that groups do not have a natural ordering. Do they have any ordering at all?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11965498
It's the Comparator that does the ordering. Brackets required here:

return (info1.getName().hashCode() + info1.getNumber()) - (info2.getName().hashCode() + info2.getNumber());

(Not quite awake yet ;-))
0
 

Author Comment

by:offline314159
ID: 11965651
Hrm.  So, if I force an ordering on them -- whether via implementing the Comparable interface or writing a quick and dirty comparator -- I'll be able to do that?  I can kludge an ordering that just arbitrarily ranks the name field and then use that.  Collections.binarySearch never uses the equals() method at all, then?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11965696
>>I can kludge an ordering that just arbitrarily ranks the name field and then use that

Yes

>>Collections.binarySearch never uses the equals() method at all, then?

Not with the method that passes a Comparator

0
 
LVL 21

Expert Comment

by:MogalManic
ID: 11965717
Another option is to use the TreeMap class.  Your key can be the "name" and "number".  To find the closest match you can use the headMap(key) and tailMap(key) methods.  To iterate through your list you can either use map.Values(), or Arrays.asList(map.values()).get(idx)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11966735
8-)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question