Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

which is cheaper  RSS feed

 
raj sekhar
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.....which is costlier ?
to evaluate the value of a string
1. have multiple if's compare against the value of a variable
OR
2. have a vector and add all the items and use contains to find the string that equals the value of a variable
will the results vary?
I dont want a lenghty code.I prefer the second approach....suggestions?
Thanks
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second approach is basically equivalent to the first, but a bit costlier. You can make it less costly by using an ArrayList instead of a Vector (don't use a Vector or Hashtable unless you're working with legacy code)However, if you are comparing to more than half a dozen Strings or so, you can do a lot better than either alternative you outlined: put the Strings into a HashSet and use contains(). Thanks to the fact that the Collections framework is interface-based, the only thing that needs changing is the class used in the initialisation code:I can only encourage you to familiarise yourself with the Collections framework, you'll be a much better developer for it...
- Peter
[This message has been edited by Peter den Haan (edited November 06, 2001).]
 
raj sekhar
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks peter !
Yes I agree that collections are lot better but can you point me where will the vector be as useful in comparison(legacy code?).Also an equivalent to Hashtable? I am used to use Hashtables when I need a value, key pairs.
Thanks
Raj
 
raj sekhar
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
probably HashMap I guess = hashtable !
Any better alternative?
Raj
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by raj sekhar:
[...] can you point me where will the vector be as useful in comparison(legacy code?).
Sorry Raj, I'm not sure I understand the question. Basically, if there's existing code that uses Vector then stick with it, but for any new stuff you'd best use ArrayList (or Collections.synchronizedList(new ArrayList()) if you need a synchronized data structure, which should be pretty rare even in multi-threaded code).
The replacement for Hashtable would be a Map implementation. In the Collections framework, you can choose between HashMap (the Hashtable equivalent) or TreeMap. The TreeMap implements the SortedMap sub-interface and can be useful if you need your keys in a sorted order. Use the Collections.synchronizedMap() decorator if you need to have a synchronized Map.
- Peter
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!