Java 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 constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75).

2

HashSet(Collection<? extends E> c)

This constructs a new set containing the elements in the specified collection.

3

HashSet(int initialCapacity)

This constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75).

4

HashSet(int initialCapacity, float loadFactor)

This constructs a new, empty set; the backing HashMap instance has the specified initial capacity and the specified load factor.

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

Methods inherited

This class inherits methods from the following classes −

  • java.util.AbstractSet
  • java.util.AbstractCollection
  • java.util.Object
  • java.util.Set

Adding element to a HashSet Example

The following example shows the usage of Java HashSet add() method to add entries to the HashSet. We've created a HashSet object of Integer. Then few entries are added using add() method and then set is printed.

package com.tutorialspoint;

import java.util.HashSet;

public class HashSetDemo {
   public static void main(String args[]) {
      
      // create hash set
      HashSet <Integer> newset = new HashSet <>();

      // populate hash set
      newset.add(1); 
      newset.add(2);
      newset.add(3);  

      // checking elements in hash set
      System.out.println("Hash set values: "+ newset);
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Hash set values: [1, 2, 3]
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy