• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Searching a LinkedList

 
David Garratt
Ranch Hand
Posts: 213
Eclipse IDE Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to use a java.util.Map which has O(1) lookup.
A linked list has O(n) lookup.
 
David Garratt
Ranch Hand
Posts: 213
Eclipse IDE Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll have a look at that,

Many thanks

Dave.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic