HashSet Implementation in Java - Java Collection Framework

In today's discussion we will talk about HashSet in details, we will come to know properties of Set collection in Java using HashSet and Examples of how to use Hash Set in java to get more from it. Java Hash Set is a useful class implementation of Java Collection framework. HashSet implements Serializable, Cloneable, Iterable<E>, Collection<E> and Set<E> interfaces and extends AbstractSet class directly.

Java Hash Set does not contains duplicate values, allows null values and does not guarantee if the order of elements will remain same over the time.
Hash Set in Java provides a huge set of implementations, some of the basic and commonly used operations are listed below with proper example.

How to create a Java Hash Set

A Java HashSet object can be obtained as shown below, gnerics specification is optional but it is always a good practice to tell JVM about the content that is going to be stored in HashSet. Use of generics eleminates the need of explicit type casting.
      // creating a new hash set with Stringlements
      HashSet<String> hashSet = new HashSet<String>(); // using generics --String in this case

How to add Elements to a Java Hash Set - add() & addAll()

Java HashSet provides two methods to add elements to it, add() adds a single object at a time while addAll() can be used to add all elements from a specified collection to the HashSet.
      // adding elements to hash set
      hashSet.add("element1"); // add elements

      // adding a collection using addAll()
      HashSet<String> hashSet2 = new HashSet<String>(); 

      //new Hash Set to add 

      // adding elements to new Hash Set
      System.out.println(hashSet2); // show elements in new hash set

      hashSet.addAll(hashSet2); // adding new hash set
     System.out.println(hashSet); // elements after adding collection using addAll

How to remove Elements from Java Hash Set - remove() & removeAll()

Hash Set in java provides two methods to remove elements from HashSet, remove() remmoves one element while removeAll() removes all elements from HashSet that are present in specified collection.
      // removing elements from hash set
      hashSet.remove("element1");// remove elements

      hashSet.removeAll(hashSet2); //removes all elements present in specified collection 

How to make Java Hash Set Empty - clear()

Java Hash Set provides clear() method to remove all elements from a HashSet, it makes HashSet an empty collection.
      // removing all elements from hash set
      System.out.println(hashSet); // return an empty hash set

How to clone a Java HashSet

Creates a shallow copy of HashSet instance but elements does not coloned themeselves. While comparing a HashSet and its clone using .equals(), it always returns a 'true'.
      //cloning hash set
      HashSet cloneHashSet = (HashSet) hashSet.clone(); // returns a shallow copy of hash set
      System.out.println(hashSet.equals(cloneHashSet)); // returns a true

How to compare Java HashSet using .equals()

It returns true if the specified object is a set and having same size as the HashSet and contains all elements that are present in HashSet comparing to.
      // returns true if specified object is a set and 
      // having same size and contains all elements

How to check if HashSet contains specified element/elements - contains() and containsAll()

HashSet provides two very important methods, contains() returns true if the specified element is present in the HashSet and containsAll() returns true if the elements in specified collection are present in HashSet.
      hashSet.contains("element"); // return true if set contains specified elementhash
      set.containsAll(hashSet2); // return true if the hash set contains all the elements specified in provided collection

How to convert Java HashSet to an Array or a String

toArray() converts all HashSet elements to an array and toString() represents a String form of HashSet.
hashSet.toArray(); // returns an array with hash set elements in it
hashSet.toString(); // returns a string presentation of hash set elements

How to use retainAll with Hash Set in Java

retainAll() method returns only those elements from HashSet that are present in the specified collection.
   hashSet.retainAll(hashSet2); // retain all elements that are present in specified collection

How to get size of a Hash Set in Java

The size of a HashSet can be obtained using .size() method, it returns an integer value equals to the number of elements in the hash set.
      // check size of hash Set
      hashSet.size(); // returns an integer value 

How to apply Iterator on a Java HashSet - iterator()

An iterator can be applied on a HashSet instance using .iterator() method, iteration of HashSet does not retain the order. hasNext() checks if the hash set contains more elements and .next() returns the next element in hash set.
Iterator<String> iterator = hashSet.iterator(); // returned an iterator on hash set -- no order  

In this blog we saw set collection in java and how to use HashSet in java, we went through examples of HashSet in java and come across the various implementations and operations over java HashSet. In upcoming blogs we will cover more topics and tricks on Java Collection Framework and its usage in Java Language.