aspose file tools*
The moose likes Beginning Java and the fly likes Hashset, Hashtable and Hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hashset, Hashtable and Hashmap" Watch "Hashset, Hashtable and Hashmap" New topic
Author

Hashset, Hashtable and Hashmap

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 357
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

Joined: Oct 02, 2003
Posts: 164
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

Joined: Aug 16, 2005
Posts: 13884
    
  10

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).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36575
    
  16
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

Joined: Oct 12, 2001
Posts: 1179

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.


Regards, Rene Larsen
Dropbox Invite
Gaurav Sainii
Greenhorn

Joined: May 18, 2009
Posts: 24
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

Joined: Oct 13, 2005
Posts: 36575
    
  16
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hashset, Hashtable and Hashmap
 
Similar Threads
HashMap Vs HashTable
java.util package
Collections
advantage of hashmap over hashtable
Remove a subset of objects at once