Java HashSet
A HashSet is used to store an unordered set of unique items of a given data type.
Syntax
import java.util.HashSet;HashSet<DataType> h = new HashSet<DataType>();
Where h is a HashSet created with the new keyword and DataType is the data type that will be stored in it. HashSet is a member of the java.util package which must be imported first.
Adding Items
New items can be added to the HashSet with the .add() method.
Syntax
hashset.add(item)
Where item is the item to be added, which should match the data type hashset was defined with.
Example
import java.util.HashSet;public class Main {public static HashSet<String> SetupHashSet() {HashSet<String> result = new HashSet<String>();result.add("Cabbage");result.add("Pizza");result.add("Sausage");result.add("Potatoes");result.add("Salad");return result;}public static void main(String[] args) {HashSet<String> food = SetupHashSet();food.add("Sausage");System.out.println(food);}}
This will output:
[Pizza, Sausage, Potatoes, Cabbage, Salad]
Note: Members of the HashSet must be unique, so adding the same item twice will have no effect, like in the example above where “Sausage” is added a second time. Also, the HashSet items have no particular order, regardless of when they were inserted.
Removing Items
Items can be removed from the HashSet with the .remove() method.
Syntax
hashset.remove(item)
Where item is the item to be removed from hashset. If item is not part of hashset the method has no effect.
Example
Changing the .main() method of the above example:
public static void main(String[] args) {HashSet<String> food = SetupHashSet();food.remove("Sausage");System.out.println(food);}
This will output:
[Pizza, Potatoes, Cabbage, Salad]
Checking For Existence of an Item
The HashSet can be checked if it contains a particular item with the .contains() method.
Syntax
boolean result = hashset.contains(item);
Where result will be true if hashset contains item as one of its members.
Example
Changing the .main() method of the above example:
public static void main(String[] args) {HashSet<String> food = SetupHashSet();if (food.contains("Nuts")) {System.out.println("Allergen warning!");} else {System.out.println("Safe to eat.");}}
This will output:
Safe to eat.
Returning the Size of a HashSet
The number of items in a HashSet can be returned with the .size() method.
Syntax
int ItemCount = hashset.size()
Where ItemCount is the number of items in hashset.
Example
Changing the .main() method of the above example:
public static void main(String[] args) {HashSet<String> food = SetupHashSet();System.out.println(food.size());food.remove("Sausage");System.out.println(food.size());}
This will output:
54
Traversing a HashSet
A HashSet can be traversed with a for-each loop. Note that when doing so, the items will not be returned in any particular order.
Example
Changing the .main() method of the above example:
public static void main(String[] args) {HashSet<String> food = SetupHashSet();for (String i : food) {System.out.println(i);}}
This will output:
PizzaSausagePotatoesCabbageSalad
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours