aspose file tools*
The moose likes Java in General and the fly likes Searching a LinkedList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Searching a LinkedList" Watch "Searching a LinkedList" New topic
Author

Searching a LinkedList

David Garratt
Ranch Hand

Joined: Aug 08, 2003
Posts: 187

I have a linked list which contains a list of object class lookupdata. Lookup data consists of two String values :- key, and value. I am wanting to determine the best way to return a "value" for any given "key". I'm aware of the indexof method, but this does not seem to help me as the only way it will find anything in the linkedlist is if I pass it a instance of lookupdata that contains both a key and value pair. I've tried messing around with overriding .toString and implementing a comparable method on my class lookupdata, but I don't think .indexof even uses them as my breakpoints never get triggered. I am currently therefor using a simple for {} loop to scan through each element of the linked list looking for the required key and returning the appropriate value. Is this the best/only way to do what I want or should I be taking a different approach.

Many thanks

Dave
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
You want to use a java.util.Map which has O(1) lookup.
A linked list has O(n) lookup.


Tony Morris
Java Q&A (FAQ, Trivia)
David Garratt
Ranch Hand

Joined: Aug 08, 2003
Posts: 187

I'll have a look at that,

Many thanks

Dave.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Searching a LinkedList
 
Similar Threads
linked list/method question
Ordered List Question
Read File
[resolved] Problem with generic methods
synchronized linked list