• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Hashtable and other collection classes using hashing

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How often are the collection classes that use hashing (i.e. Hashtable, HashSet, HashMasp) used in job-related projects? Why would these classes be selected to store objects over the other collection classes that do not use hashing?
Thanks.
Mansi
 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mansi,
It isn't a question of statistics (how often is class 'X' used...), but rather a matter of design.
For instance, if you need to cycle through the entire collection, either in order or not, then you would use a list of some sort.
If you will need to check for the existance of specific objects frequently, then you should use a data structure based on hashing.
If you are concerned with the uniqueness of the items in the collection, then you want a structure based on a Set.
I suggest that you find a good general reference on data structures if you really want to dig into this topic further. And this site is still a great introduction to the topic in the Java context.
[ July 10, 2003: Message edited by: Philip Shanks ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The short answer is that HashMap is used quite a lot. HashSet is less common, but not unusual. Which one you use will depend on your needs.
First, decide if you need a List, Map, or Set. If you need a Map, HashMap is the standard solution. The main alternative is TreeMap - it's slower, but has the additional property of keeping its entries sorted, whihc may be useful. (HashMap is faster, but if you iterate it's like reading things in random order. Often this isn't a problem though.) Similarly if you need a Set, HashSet is the standard solution. Or you can use a TreeSet which is, again, slower, but keeps its elements sorted.
For instance, if you need to cycle through the entire collection, either in order or not, then you would use a list of some sort.
Well, you can iterate through any Collection or Map, not just a list. And it's always an O(n) operation for the standard classes if you do it right. No big difference in classes here. Basically, use a List if you want to be able to refer to entries by number, or if you don't have any compelling reason to choose a Set or Map - a List (typically ArrayList) is a good default.
 
Mansi Vyas
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
 
Mansi Vyas
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I definitely need to use a Map. I need to have some way of storing name/value pairs. Is there an effecient way of doing this without using a hashing algorithm?
Can I create my own class that implements Map and then uses two Vectors, one for keys and one for values (similar to a dictionary)?
Mansi
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your concern is the hashing algorithm itself, pick up a copy of Joshua Bloch's "Effective Java" (link to the bunkhouse)
I suggest you buy a copy even if you don't need it for the hashCode template
Dave
 
reply
    Bookmark Topic Watch Topic
  • New Topic