• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hashset, Hashtable and Hashmap

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • 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
 
Yosi Hendarsjah
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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).
 
Campbell Ritchie
Sheriff
Pie
Posts: 49812
69
  • Mark post as helpful
  • send pies
  • 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.
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Gaurav Sainii
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 49812
69
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic