Collections

HashSet methods Java Code With Examples

Table of Contents

Program

import java.util.HashSet;  
public class HashSetExamples
{  
    public static void main(String[] args) 
    {  
          HashSet<Integer> hset = new HashSet<Integer>();        
          
          //add()
          hset.add(121);   
          hset.add(111);  
          hset.add(151);   
          hset.add(548);
          hset.add(557);
          System.out.println("Hash set Elements: "+ hset);  					
          
          //remove()
          hset.remove(111);                               					
          
          //contains()
          System.out.println("\nAfter removing:"+hset);
          System.out.println("\nDoes the set contains 151? " +hset.contains(151));		
          System.out.println("\nDoes the set contains 200? " +hset.contains(200));
          System.out.println("\nDoes the set contains 548? " +hset.contains(548));
          
          //toArray()
          Object[] arr = hset.toArray();							
          System.out.println("\nThe array is:");
       for (int i = 0; i < arr.length; i++)
	{
           System.out.println(arr[i]);
	}  
           //clear()  
          hset.clear();
          System.out.println("\nAfter using Clear set:"+hset);                    		
     }  
}

Output

Hash set Elements: [548, 151, 121, 557, 111]

After removing:[548, 151, 121, 557]

Does the set contains 151? true

Does the set contains 200? false

Does the set contains 548? true

The array is:
548
151
121
557

After using Clear set:[]

Description

public boolean add​(E e)

Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if this set contains no element e2 such that Objects.equals(e, e2). If this set already contains the element, the call leaves the set unchanged and returns false.

Specified by:

add in interface Collection<E>

Specified by:

add in interface Set<E>

Overrides:

add in class AbstractMapCollection<E>

Parameters:

e – element to be added to this set

Returns:

true if this set did not already contain the specified element

public boolean remove​(Object o)

Removes the specified element from this set if it is present. More formally, removes an element e such that Objects.equals(o, e), if this set contains such an element. Returns true if this set contained the element (or equivalently, if this set changed as a result of the call). (This set will not contain the element once the call returns.)

Specified by:

remove in interface Collection<E>

Specified by:

remove in interface Set<E>

Overrides:

remove in class AbstractMapCollection<E>

Parameters:

o – object to be removed from this set, if present

Returns:

true if the set contained the specified element

public boolean contains​(Object o)

Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that Objects. equals(o, e).

Specified by:

contains in interface Collection<E>

Specified by:

contains in interface Set<E>

Overrides:

contains in class AbstractMapCollection<E>

Parameters:

o – element whose presence in this set is to be tested

Returns:

true if this set contains the specified element

public Object[] toArray()

Returns an array containing all of the elements in this collection. If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order. The returned array’s runtime component type is Object.
The returned array will be “safe” in that no references to it are maintained by this collection. (In other words, this method must allocate a new array even if this collection is backed by an array). The caller is thus free to modify the returned array.

Specified by:

toArray in interface Collection<E>

Implementation Requirements:

This implementation returns an array containing all the elements returned by this collection’s iterator, in the same order, stored in consecutive elements of the array, starting with index 0. The length of the returned array is equal to the number of elements returned by the iterator, even if the size of this collection changes during iteration, as might happen if the collection permits concurrent modification during iteration. The size method is called only as an optimization hint; the correct result is returned even if the iterator returns a different number of elements.

This method is equivalent to:

 List<E> list = new ArrayList<E>(size());
 for (E e : this)
     list.add(e);
 return list.toArray();

Returns:

an array, whose runtime component type is Object, containing all of the elements in this collection

public void clear()

Removes all of the elements from this collection (optional operation). The collection will be empty after this method returns.

Specified by:

clear in interface Collection<E>

Implementation Requirements:

This implementation iterates over this collection, removing each element using the Iterator.remove operation. Most implementations will probably choose to override this method for efficiency.
Note that this implementation will throw an UnsupportedOperationException if the iterator returned by this collection’s iterator method does not implement the remove method and this collection is non-empty.

Throws:

UnsupportedOperationException – if the clear operation is not supported by this collection