Easy Tutorial
For Competitive Exams

Java Programming HashSet

What is Hashset?

This class implements the Set interface.

HashSet doesn’t maintain any order, the elements would be returned in any random order.

HashSet allows null values but doesn’t allow duplicates.

If you try to add a duplicate element in HashSet, the old value would be overwritten.

HashSet is non-synchronized.

How to perform Basic Operations in HashSet ?

The Basic Operations are:

  • creating object, adding elements
  • verifying whether the hashset is empty or not
  • removing an element
  • size of the hashset
  • to check whether an object exists or not

Basic Operations

import java.util.HashSet;
 
public class MyBasicHashSet {
 
    public static void main(String a[]){
        HashSet hs = new HashSet();
        //add elements to HashSet
        hs.add("first");
        hs.add("second");
        hs.add("third");
        System.out.println(hs);
        System.out.println("Is HashSet empty? "+hs.isEmpty());
        hs.remove("third");
        System.out.println(hs);
        System.out.println("Size of the HashSet: "+hs.size());
        System.out.println("Does HashSet contains first element? "+hs.contains("first"));
    }
}
Try it Yourself

How to iterate through HashSet?

You can iterate through HashSet by getting Iterator object.

By calling iterator() method, you can get Iterator object.

Example

import java.util.HashSet;
import java.util.Iterator;
 
public class MyHashSetRead {
 
    public static void main(String a[]){
        HashSet hs = new HashSet();
        //add elements to HashSet
        hs.add("first");
        hs.add("second");
        hs.add("third");
        Iterator itr = hs.iterator();
        while(itr.hasNext()){
            System.out.println(itr.next());
        }
    }
}
Try it Yourself

Is it possible to add null value in HashSet?

HashSet allows null values however if you insert more than one nulls it would still return only one null value.

Example

import java.util.*;
    class Output {
        public static void main(String args[]) {
            HashSet obj = new HashSet();
            obj.add(null);
            obj.add("B");
            obj.add("A");
            obj.add("A");
            obj.add(null);
            obj.add("C");
            obj.add(null);
            System.out.println(obj+""+obj.size());
        }
    }
Try it Yourself

How to copy Set content to another HashSet?

By calling addAll() method you can copy another collection to HashSet object.

Example

import java.util.HashSet;
 
public class MyHashSetCopy {
 
    public static void main(String a[]){
        HashSet hs = new HashSet();
        //add elements to HashSet
        hs.add("first");
        hs.add("second");
        hs.add("third");
        System.out.println(hs);
        HashSet subSet = new HashSet();
        subSet.add("s1");
        subSet.add("s2");
        hs.addAll(subSet);
        System.out.println("HashSet content after adding another collection:");
        System.out.println(hs);
    }
}
Try it Yourself

How to delete all elements from HashSet?

By calling clear() method, we can remove all elements from HashSet at once.

Example

public class MyHashSetClear {
 
    public static void main(String a[]){
        HashSet hs = new HashSet();
        //add elements to HashSet
        hs.add("first");
        hs.add("second");
        hs.add("third");
        System.out.println("My HashSet content:");
        System.out.println(hs);
        System.out.println("Clearing HashSet:");
        hs.clear();
        System.out.println("Content After clear:");
        System.out.println(hs);
    }
}
Try it Yourself

How to compare two sets and retain elements which are same on both sets?

To compare two sets, and retain the values which are common on both set objects.

By calling retainAll() method you can do this operation.

Example

import java.util.HashSet;
 
public class MyHashSetRetain {
 
    public static void main(String a[]){
        HashSet hs = new HashSet();
        //add elements to HashSet
        hs.add("first");
        hs.add("second");
        hs.add("third");
        hs.add("apple");
        hs.add("rat");
        System.out.println(hs);
        HashSet subSet = new HashSet();
        subSet.add("rat");
        subSet.add("second");
        subSet.add("first");
        hs.retainAll(subSet);
        System.out.println("HashSet content:");
        System.out.println(hs);
    }
}
Try it Yourself

Solve this!!

#20133,20134,20135,20136
Share with Friends