Java HashSet

The HashSet implements the Set interface. Every item is unique in a HashSet. It is a part of the java.util package i.e., you need to import the java.util.HashSet package.

Let us see some LinkedList operation/ methods with examples:

  • Create a HashSet
  • Size of a Hashset
  • Loop through the HashSet
  • Remove an item from a HashSet
  • Check if an item exists in a HashSet

Let us first see how to create a HashSet in Java:

Create a HashSet

To create a HashSet, set the type in angular braces i.e., use the wrapper class. For example, for creating a HashSet with string elements, use <String>:

HashSet<String> mySet = new HashSet <String>();

Let us now see an example and create a HashSet in Java with string elements:

import java.util.HashSet;

public class Main {

  public static void main(String[] args) {
   
   HashSet<String> sports = new HashSet<String>();
    
   // Adding string elements
   sports.add("Cricket");
   sports.add("Football");
   sports.add("Tennis");
   sports.add("Badminton");
   sports.add("Basketball");
   sports.add("Volleyball");
    
   // Display the HashSet
   System.out.println("HashSet\n"+sports);
  }
}

Output

HashSet
[Cricket, Tennis, Volleyball, Badminton, Basketball, Football]

Size of HashSet

To get the size of HashSet i.e., how many elements does it have, use the size() method. Let us see an example:

import java.util.HashSet;

public class Main {

  public static void main(String[] args) {
   
   HashSet<String> sports = new HashSet<String>();
    
   // Adding string elements
   sports.add("Cricket");
   sports.add("Football");
   sports.add("Tennis");
   sports.add("Badminton");
   sports.add("Basketball");
   sports.add("Volleyball");
    
   // Display the HashSet
   System.out.println("HashSet\n"+sports);
   
   // Size of the HashSet
   System.out.println("\nHashSet Size = "+sports.size());
  }
}

Output

HashSet
[Cricket, Tennis, Volleyball, Badminton, Basketball, Football]

HashSet Size = 6

Loop through the HashSet

Use the for-each loop to loop through the ArrayList in Java. Let us see an example:

import java.util.HashSet;

public class Main {

  public static void main(String[] args) {
   
   HashSet<String> sports = new HashSet<String>();
    
   // Adding string elements
   sports.add("Cricket");
   sports.add("Football");
   sports.add("Tennis");
   sports.add("Badminton");
   sports.add("Basketball");
   sports.add("Volleyball");
    
   // Display the HashSet
   System.out.println("HashSet\n"+sports);
   
   // Loop through the HashSet
   for (String s : sports) {
     System.out.println(s);
  }
 }
}

Output

HashSet
[Cricket, Tennis, Volleyball, Badminton, Basketball, Football]
Cricket
Tennis
Volleyball
Badminton
Basketball
Football

Remove an item from a HashSet

To remove an element from a HashSet, use the remove() method. Set the element you want to remove as a parameter of the remove () method. For example, to remove the element “Football”, set it as:

sports.remove("Football");

Let us now see an example of removing an element from a HashSet in Java:

import java.util.HashSet;

public class Main {

  public static void main(String[] args) {
   
   HashSet<String> sports = new HashSet<String>();
    
   // Adding string elements
   sports.add("Cricket");
   sports.add("Football");
   sports.add("Tennis");
   sports.add("Badminton");
   sports.add("Basketball");
   sports.add("Volleyball");
    
   // Display the HashSet
   System.out.println("HashSet\n"+sports);
   
   // Remove an element using the remove() method
   sports.remove("Football");

   // Display the updated HashSet
   System.out.println("Updated HashSet\n"+sports);
 }
}

Output

HashSet
[Cricket, Tennis, Volleyball, Badminton, Basketball, Football]
Updated HashSet
[Cricket, Tennis, Volleyball, Badminton, Basketball]

Check if an item exists in a HashSet

The contains() method is used in a HashSet to check if an item exists. Let us see an example:

import java.util.HashSet;

public class Main {

  public static void main(String[] args) {
   
   HashSet<String> sports = new HashSet<String>();
    
   // Adding string elements
   sports.add("Cricket");
   sports.add("Football");
   sports.add("Tennis");
   sports.add("Badminton");
   sports.add("Basketball");
   sports.add("Volleyball");
    
   // Display the HashSet
   System.out.println("HashSet\n"+sports);
   
   // HashSet after removing an element
   System.out.println("\nDoes the HashSet contain an element Tennis?\n"+sports.contains("Tennis"));
 }
}

Output

HashSet
[Cricket, Tennis, Volleyball, Badminton, Basketball, Football]

Does the HashSet contain an element Tennis?
True

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Java ArrayList
Java HashMap
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment