Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to use HashSet in Java with example ?.

Program to demonstrate how to implement HashSet class in java.util.*; package along with its operations :

package com.hubberspot.collections.example;

import java.util.*;

public class HashSetExample
{
 public static void main(String[] args)
 {
  HashSet hashset=new HashSet();
  hashset.add("B");
  hashset.add("A");
  hashset.add("F");

  System.out.println("The contents of set : "
     + hashset);
  
  hashset.add("E");
  hashset.remove("A");

  System.out.println("The contents of set : "
     + hashset);

  TreeSet treeSet = new TreeSet(hashset);

  System.out.println("The tree set contents : "
     + treeSet);
    
 } 
}

Output of the program :


 
© 2021 Learn Java by Examples Template by Hubberspot