Solved

Searching a list using a generic instance of a class

Posted on 2004-09-02
11
238 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 125 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
printing a file in reverse order is easy in recursion rather than in iteration 3 56
Java DateChooser? 3 36
more than one jdk and one jre 1 41
MySQL  on Tomcat 8 45
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

770 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