• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Hashset, Hashtable and Hashmap

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hashtable can store key - value pairs, but it does not allow null values to be stored in it. (is this right ?)

Hashmap can store key-value pairs and also allows null values to be stored in it. (is this right ?)

Does Hashset also store key-value pairs ? If yes, then whats the difference between Hashset and Hashtable ? or Hashset and Hashmap ? If it does not store key-value pairs and stores only objects, then whats the diffeence between Hashset and Arraylist ?

Please correct me ,where mu understanding is wrong.

I tried to understand from API docs, but got more confused.

Please use non technical terminology while explaining.

Thanks
 
Ranch Hand
Posts: 168
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HashSet objects, and any Set objects only store objects, not key-value pairs. And in a Set, we can't have two or more objects that are equals. Also, we can only have at most one null object in a Set. In a List object, we can have any numbers of equal objects and any number of null objects.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nirjari patel wrote:Hashtable can store key - value pairs, but it does not allow null values to be stored in it. (is this right ?)

Hashmap can store key-value pairs and also allows null values to be stored in it. (is this right ?)


You can find the answer quickly by writing a small program to try it out. Note that it's HashMap, not Hashmap (case is important in Java).

Note that Hashtable is an old, legacy collection class. Since Java 1.2 (a LONG time ago) is has been more or less replaced by HashMap. Always use HashMap instead of Hashtable (unless there's a very special reason that you have to use Hashtable).

nirjari patel wrote:Does Hashset also store key-value pairs ? If yes, then whats the difference between Hashset and Hashtable ? or Hashset and Hashmap ? If it does not store key-value pairs and stores only objects, then whats the diffeence between Hashset and Arraylist ?


No, HashSet doesn't store key-value pairs.

A Set is a collection of unique objects that isn't ordered: you can think of it as a bag of things, the things are in no particular order, and each thing can be only in it once (no duplicates).

A List is a sequence of objects: it has a clear order, and it can contain objects that are equal (the same thing can appear more than once in a List).

HashSet is a particular implementation of a Set, and ArrayList is a particular implementation of a List. There are other implementations (for example TreeSet and LinkedList).
 
Marshal
Posts: 80634
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would do well to look up the mathematical constructs set function and sequence. Have a look at this Java™ Tutorials section, as well as the documentation for List, Map and Set.
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nirjari patel wrote:Hashtable can store key - value pairs, but it does not allow null values to be stored in it. (is this right ?)

Hashmap can store key-value pairs and also allows null values to be stored in it. (is this right ?)



Yes, you are correct on both..

The other difference is that Hashtable is synchronized, and HashMap isn't - the same with the old Vector API which is also synchronized, and the 'successor' List and Set are not.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HashSet in Java is a class that implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.

Does HashSet also store key-value pairs ?


No.

If it does not store key-value pairs and stores only objects, then whats the diffeence between Hashset and Arraylist ?


HashSet allows only unique elements while ArrayList allows duplicates.
HashSet has sequential access while ArrayList supports random access.


I hope it solves your doubts?


 
Campbell Ritchie
Marshal
Posts: 80634
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HashSet is a Set and ArrayList is a List/Sequence. You need to do some basic reading rather than asking lots of little questions. I have already told you what to look for.
reply
    Bookmark Topic Watch Topic
  • New Topic