Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Java Miscellaneous

Advanced Java

Java APIs & Frameworks

Java Useful Resources

Java - The HashSet Class



Introduction

The Java HashSet class implements the Set interface, backed by a hash table.Following are the important points about HashSet −

  • This class 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.

Class declaration

Following is the declaration for java.util.HashSet class −

public class HashSet<E>
   extends AbstractSet<E>
   implements Set<E>, Cloneable, Serializable

Parameters

Following is the parameter for java.util.HashSet class −

E − This is the type of elements maintained by this set.

Class constructors

Sr.No. Constructor & Description
1

HashSet( )

This constructor constructs a default HashSet.

2

HashSet(Collection c)

This constructor initializes the hash set by using the elements of the collection c.

3

HashSet(int capacity)

This constructor initializes the capacity of the hash set to the given integer value capacity. The capacity grows automatically as elements are added to the HashSet.

4

HashSet(int capacity, float fillRatio)

This constructor initializes both the capacity and the fill ratio (also called load capacity) of the hash set from its arguments.

Here the fill ratio must be between 0.0 and 1.0, and it determines how full the hash set can be before it is resized upward. Specifically, when the number of elements is greater than the capacity of the hash set multiplied by its fill ratio, the hash set is expanded.

Apart from the methods inherited from its parent classes, HashSet defines following methods −

Sr.No. Method & Description
1 boolean add(E e)

This method adds the specified element to this set if it is not already present.

2 void clear()

This method removes all of the elements from this set.

3 Object clone()

This method returns a shallow copy of this HashSet instance, the elements themselves are not cloned.

4 boolean contains(Object o)

This method returns true if this set contains the specified element.

5 boolean isEmpty()

This method returns true if this set contains no elements.

6 Iterator<E> iterator()

This method returns an iterator over the elements in this set.

7 boolean remove(Object o)

This method removes the specified element from this set if it is present.

8 int size()

This method returns returns the number of elements in this set(its cardinality).

9 Spliterator<E> spliterator()

This method returns a late-binding and fail-fast Spliterator over the elements in this set.

Example

The following program illustrates several of the methods supported by HashSet −

import java.util.*;
public class HashSetDemo {

   public static void main(String args[]) {
      // create a hash set
      HashSet hs = new HashSet();
      
      // add elements to the hash set
      hs.add("B");
      hs.add("A");
      hs.add("D");
      hs.add("E");
      hs.add("C");
      hs.add("F");
      System.out.println(hs);
   }
}

This will produce the following result −

Output

[A, B, C, D, E, F]

Methods inherited

This class inherits methods from the following classes −

  • java.util.AbstractSet
  • java.util.AbstractCollection
  • java.util.Object
  • java.util.Set
java_collections.htm
Advertisements