21 Jul 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>:
1 2 3 |
HashSet<String> mySet = new HashSet <String>(); |
Let us now see an example and create a HashSet in Java with string elements:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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
1 2 3 4 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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
1 2 3 4 5 6 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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
1 2 3 4 5 6 7 8 9 10 |
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:
1 2 3 |
sports.remove("Football"); |
Let us now see an example of removing an element from a HashSet in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
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
1 2 3 4 5 6 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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
1 2 3 4 5 6 7 |
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
No Comments