How to use a HashSet

Description:

  • HashSet class is a concrete implementation of Set interface.
  • It creates a collection that uses a hash table for storage.
  • Hash table stores information by using a mechanism called hashing. In hashing, the informational content of a key is used to determine a unique value, called its hash code. The hash code is then used as an index at which the data associated with the key is stored.
  • The transformation of key into its hash code is performed automatically. You never see the hash code itself. The advantage of hashing is that it allows the execution time of basic operation, such as add(), contains(), remove(), and size() to remain constant even for large sets.

WARNING:

  • HashSet is not synchronized. If more than one thread wants to access it at the same time then it must be synchronized externally.
Constructor Description
HashSet() Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75).
HashSet(Collection<? extends E> c) Constructs a new set containing the elements in the specified collection.
HashSet(int initialCapacity) Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75).
HashSet(int initialCapacity, float loadFactor) Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and the specified load factor.

Method Description
add(E e) Adds the specified element to this set if it is not already present.
clear() Removes all of the elements from this set.
clone() Returns a shallow copy of this HashSet instance: the elements themselves are not cloned.
contains(Object o) Returns true if this set contains the specified element.
isEmpty() Returns true if this set contains no elements.
iterator() Returns an Iterator<E> over the elements in this set.
remove(Object o) Removes the specified element from this set if it is present.
size() Returns the number of elements in this set (its cardinality).

This code shows the use of HashSet. This will identify the number of duplicate words in a String. The String is passed as command line arguments.

Add() method of the HashSet adds the object into the storage if it is not already present.

import java.util.*;

public class FindDups {

public static void main(String[] args) {

Set s = new HashSet();

for(int i=0; i<args.length;i++){
if(!s.add(args[i]))
System.out.println("Duplicate detected : " + args[i]);
}

System.out.println(s.size() + " distinct words detected : " + s );
}
}

Run the program: C:\> java FindDups i came i came i conquered

Output Screen :

Duplicate detected: i
Duplicate detected: i
4 distinct words detected : [came,saw,conquered,i]

http://www.j2ee.me/javase/6/docs/api/java/util/HashSet.html