Friday, September 07, 2007

How to use HashSet

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.

HashSet is not synchronized. If more than one thread wants to access it at the same time then it must be synchronized externally.

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 add 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 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]

No comments: