HashSet in Java is a class from the Collections Framework. It allows you to store multiple values in a collection using a hash table. The hash table stores the values in an unordered method with the help of hashing mechanism. Once you import the java.util.HashSet package, here’s the syntax to create a HashSet in Java:

HashSet<data_type> name = new HashSet (capacity, loadFactor)

In the above syntax:

Data_type: It is the data type of values you want to store in the HashSet.

Capacity: It is the number of elements the HashSet will store.

loadFactor: It accepts a value between 0.00 to 1.00. Suppose a loadFactor is set to 0.70, the elements of the HashSet will be moved to another hash table that is double the size of the original once it is filled by 70%.

Both capacity and loadFactor parameters are optional. If you don’t provide the arguments, it initializes the HashSet with the default values. Now that we've understood the declaration syntax, let’s look at some of HashSet’s essential features.

Become a Certified UI UX Expert in Just 5 Months!

UMass Amherst UI UX BootcampExplore Program
Become a Certified UI UX Expert in Just 5 Months!

Features of HashSet in Java

Some of the most important features of HashSet include:

  • Allows null value
  • It stores the elements using the hashing mechanism
  • It is a non-synchronized class
  • Elements are inserted and identified using hash code
  • Only contains unique elements
  • Most useful in the search operations
  • Default capacity and loadFactor values are 16 and 0.75, respectively
  • It uses the hash table data structure

HashSet Class Hierarchy

It extends the AbstractSet class and implements the Set Interface. The set interface then further extends the collections and iterable interfaces in the Java HashSet hierarchy.

Constructors and Methods of the HashSet Class

In this section, we will be looking at various constructors and methods of HashSet in Java and what we use them for.

Different Constructors of HashSet in Java

These are the different constructors used to construct and initialize a HashSet.

Constructor Name

Description

HashSet()

Constructs default HashSet with default capacity and loadFactor arguments.

HashSet (int capacity)

Constructs a HashSet in Java with an initial capacity value, which can increase automatically.

HashSet (int capacity, float loadFactor)

Constructs a HashSet with the mentioned capacity and loadFactor values.

HashSet (Collection c)

Constructs a HashSet and initializes it with the elements from the Collection C. (C is the name of the collection and can be changed)

Different Methods of HashSet in Java

These are the different methods used to perform various options on a HashSet.

Modifier & Type

Method Name

Description

boolean

add(E e)

Adds an element of type E to it, but only if it is not already present.

void

clear()

Clears the entire HashSet by removing all the elements.

object

clone()

Creates and returns a clone (shallow copy) of the HashSet instance. (Instances are cloned and not the actual elements).

boolean

contains(Object o)

It returns true if the specified element is found in it.

boolean

isEmpty()

It returns true if it is Empty (no elements).

Iterator

iterator()

Returns an iterator over all its values.

boolean

remove(Object o)

If found, it removes the specified element from the HashSet.

Int

size()

Returns the number of elements.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Having understood the basics of HashSet in Java, it’s time for you to look at some examples to understand better how it works. You will find various examples in this tutorial, each demonstrating the use of at least one of HashSet’s methods.

  • Example: Using the add(E e) method

hashset-ex1

  • Example: Using the clear() method

hashset-ex2

  • Example: Using the contains(Object o) method

hashset-ex3

  • Example: Using the isEmpty() method

hashset-ex4

  • Example: Using the iterator() method

hashset-ex5

  • Example: Using the remove(Object o) method

hashset-ex6

  • Example: Using the size() method

hashset-ex7

  • Example: Adding elements from another collection (ArrayList)

hashset-ex8

  • Example: Trying to add a duplicate item (HashSet in Java does not accept the same items; hence, it will throw an error).

hashet-ex-9

With that, we have come to an end in this Java HashSet article.

Get a firm foundation in Java, the most commonly used programming language in software development with the Java Certification Training Course.

Conclusion

We hope the Java HashSet concepts are clear to you by now.

Java has long been and currently is a widely popular programming language across the world. Pursuing a career in Java development can help you grab a large package in the software development field. You can take our Java Full Stack Developer Course to learn more advanced concepts such as HashSet in Java.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 17 Jun, 2024

6 Months$ 8,000
Full Stack Developer - MERN Stack

Cohort Starts: 30 Apr, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 1 May, 2024

11 Months$ 1,499
Full Stack Java Developer

Cohort Starts: 14 May, 2024

6 Months$ 1,449