HashSet In Java: Features, Hierarchy, Constructors, Methods, and More

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.

FREE Java Certification Training

Learn A-Z of Java like never beforeEnroll Now
FREE Java Certification Training

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.

Full Stack Java Developer Course

The Gateway to Master Web DevelopmentExplore Course
Full Stack Java Developer Course

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.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.