Tutorial Playlist
191 Lessons1. Introduction to Java
2. What is Java?
3. History of Java
4. Java Tutorial for Beginners
5. How Do Java Programs Work?
6. JDK in Java
7. C++ Vs Java
8. Java vs. Python
9. Java vs. JavaScript
10. From Java Source Code to Executable
11. How to Install Java in Linux
12. How to Install Java in Windows 10
13. Java Hello World Program
14. Structure of Java Program and Java Syntax
15. Operators in Java
16. Java If-else
17. Switch Case In Java
18. Loops in Java
19. Infinite loop in Java
20. For Loop in Java
21. For Each Loop in Java
22. Constructor in Java
23. Constructor Overloading in Java
24. Copy Constructor in Java
25. Default Constructor in Java
26. Parameterized Constructors in Java
27. Constructor Chaining In Java
28. Finalize Method in Java
29. Static Method in Java
30. Equals Method in Java
31. Abstract Method in Java
32. toString() Method in Java
33. Difference between equals method in Java
34. Inheritance in Java
35. Multiple Inheritance in Java
36. Hierarchical Inheritance in Java
37. Java Classes and Objects
38. Scanner Class in java
39. All classes in java are inherited from which class
40. What is Nested Class in Java
41. POJO Class in Java
42. Anonymous Class in Java
43. Final Class in Java
44. Object Class in Java
45. Packages in Java
46. Access Modifiers in Java
47. Static Keyword In Java
48. Final Keyword in Java
49. Checked and Unchecked Exceptions in Java
50. User Defined Exception in Java
51. Error vs. Exception in Java
52. Java Collection
53. Collections in Java
54. Garbage Collection in Java
55. Generics In Java
56. Java Interfaces
57. Functional Interface in Java
58. Marker Interface in Java
59. Streams in Java
60. Byte stream in java
61. File Handling in Java
62. Thread in Java
63. Thread Lifecycle In Java
64. Daemon Thread in Java
65. Thread Priority in Java
66. Deadlock in Java
67. String Pool in Java
68. Java Database Connectivity(JDBC)
69. Design Patterns in Java
70. Functional Programming in Java
71. OOP vs Functional vs Procedural
72. Heap Memory and Stack Memory in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
76. Hibernate Framework
77. JUnit Testing
78. How to Install Eclipse IDE for Java?
79. Command line arguments in Java
80. Jar file in Java
81. Java Clean Code
82. OOPs Concepts in Java
83. Java OOPs Concepts
84. Overloading vs Overriding in Java
85. Java 8 features
86. String in Java
87. String to int in Java
88. Why String Is Immutable in Java?
89. Primitive Data Types in Java
90. Non-Primitive Data Types in Java
91. This and Super Keyword in Java
92. HashMap in Java
93. Comparable And Comparator in Java
94. Type Casting in Java
95. Arrays Sort in Java with Examples
96. Variable Hiding and Variable Shadowing in Java
97. Enum in Java
98. Substring in Java
99. Pattern Programs in Java
100. Hashcode in Java
101. What is ByteCode in Java?
102. How To Take Input From User in Java
103. GCD of Two Numbers in Java
104. Linked List in Java
105. Arithmetic Operators in Java
106. Conditional Operators in Java
107. Stack and Queue in Java
108. Array Length in Java
109. Number Pattern Program in Java
110. Split in java
111. Map In Java
112. Difference Between Throw and Throws in Java
113. Difference Between Data Hiding and Abstraction
114. HashSet in Java
Now Reading
115. String Length in Java
116. Factorial Using Recursion in Java
117. DateFormat in Java
118. StringBuilder Class in java
119. Instance variables in Java
120. Java List Size
121. Java APIs
122. Reverse an Array in Java
123. StringBuffer and StringBuilder Difference in Java
124. Java Program to Add Two Numbers
125. String to Array in Java
126. Regular Expressions in Java
127. Identifiers in Java
128. Data Structures in Java
129. Set in Java
130. Pass By Value and Call By Reference in Java
131. Try Catch in Java
132. Bubble Sort in Java
133. Caesar Cipher Program in Java
134. Queue in Java
135. Object Creation in Java
136. Multidimensional Array in Java
137. How to Read a File in Java
138. String Comparison in Java
139. Volatile Keyword in Java
140. Control Statements in Java
141. Jagged Array in Java
142. Two-Dimensional Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
147. Matrix Multiplication in Java
148. Static Variable in Java
149. Event Handling in Java
150. parseInt in Java
151. Java ArrayList forEach
152. Abstraction in Java
153. String Input in Java
154. Logical Operators in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
159. Stringtokenizer in java
160. Implementing and Manipulating Abs in Java
161. Char array to string in java
162. Convert Double To String In Java
163. Deque in Java
164. Converting a List to an Array in Java
165. The Max function in java
166. Removing whitespace from string in java
167. String arrays in Java
168. Strings in Java Vs Strings in Cpp
169. Sum of digits of a number in Java
170. Art of Graphical User Interfaces
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
175. Difference Between Java and Python
176. Square Root in Java
177. Reverse A String in Java
178. Even Odd Program in Java
179. Fibonacci Series in Java
180. Prime Number Program in Java
181. Java Program to Print Prime Numbers in a Given Range
182. Java Leap Year Program
183. Swapping of Two Numbers in Java
184. LCM of Two Numbers in Java
185. Math.sqrt() Function in Java
186. Area of Triangle in Java
187. Sort a String In Java
188. Factorial Program in Java
189. Javafx
190. Lambda expression in java
191. Setup Java Home and IDE on macOS
The Java Collections Framework's HashSet class in Java offers an implementation of the Set interface. It is used to keep an assortment of distinctive components. The hash table data structure on which HashSet is built enables effective insertion, deletion, and retrieval operations. The whole Java HashSet, including its definition, attributes, hierarchy, constructors, methods, and use examples, are covered in this article.
Java's HashSet class is frequently used to represent an element-free, unordered collection. It implements the Set interface and extends the AbstractSet class. A hash table, which is used by HashSet to hold its items, offers constant-time performance for operations like add, delete, and contain. The sequence of the components is not fixed and is subject to vary over time.
Java's HashSet class implements the Set interface and offers a quick way to store distinct components. It stores and retrieves items using a hash table on the inside. Based on their hash codes, items may be quickly accessed using the hash table, which offers constant-time performance for simple operations. Here is an illustration of how to use HashSet:
javaCopy code
import java.util.HashSet;
public class HashSetExample {
public static void main(String[] args) {
HashSet<String> fruits = new HashSet<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Apple"); // Duplicate element
System.out.println(fruits);
}
}
Output:
csharpCopy code
[Orange, Banana, Apple]
In the illustration, a HashSet called "fruits" is created, and numerous components are added to it. It is important to note that HashSet does not accept duplicates. Therefore when we attempt to add the element "Apple," it is disregarded. The output shows the elements in an unordered manner.
Unique Elements: HashSet does not allow duplicate elements. It ensures that each element in the set is unique based on the equals() method.
Unordered Collection: HashSet does not guarantee any specific order for its elements. The order may change over time and is influenced by the internal hash table structure.
Fast Performance: HashSet provides constant-time performance for basic operations like add, remove, and contain. The hash table data structure enables efficient retrieval and manipulation of elements.
List and Set are both interfaces in the Java Collections Framework, but they have some fundamental differences.
Duplicate Elements: List allows duplicate elements, whereas Set does not. You can have multiple occurrences of the same element in a List, while a Set ensures uniqueness by rejecting duplicates.
Order of Elements: The list preserves the order of elements and allows positional access. You can access elements by their index in a List. On the other hand, Set does not maintain any specific order. The order of elements in a Set may change over time.
Implementation: ArrayList and LinkedList are two implementations of the List interface that provide various performance and feature trade-offs. HashSet, one of the many implementations of Set, offers quick performance for fundamental operations at the expense of unordered items.
The HashSet class in Java is part of the Java Collections Framework and inherits properties and methods from several classes and interfaces. The class hierarchy of HashSet is as follows:
markdownCopy code
java.lang.Object
└── java.util.AbstractCollection
└── java.util.AbstractSet
└── java.util.HashSet
The HashSet class directly extends the AbstractSet class, which provides a skeletal implementation of the Set interface. AbstractSet, in turn, extends the AbstractCollection class, which serves as a base implementation for various collection classes.
The declaration of the HashSet class in Java is as follows:
javaCopy code
public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable {
// Class implementation
}
Here, E represents the type of elements that the HashSet will hold. The class implements the Set interface and extends the AbstractSet class, providing implementations for various methods.
The HashSet class provides several constructors to create instances of HashSet with different initial capacities and load factors. Here are some commonly used constructors:
HashSet(): Constructs an empty HashSet with the initial default capacity (16) and load factor (0.75).
Creates a HashSet from the items of the given collection using the function HashSet(Collection? extends E> c).
HashSet(int initialCapacity): Creates a new HashSet that is empty and has a default load factor of 0.75 and the supplied initial capacity.
An empty HashSet with the supplied initial capacity and load factor is created by using HashSet(int initialCapacity, float loadFactor).
Let's look at an illustration of how to use these constructors:
javaCopy code
import java.util.HashSet;
public class HashSetConstructorExample {
public static void main(String[] args) {
HashSet<String> fruits = new HashSet<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
HashSet<String> copySet = new HashSet<>(fruits); // Using the constructor HashSet(Collection<? extends E> c)
System.out.println(copySet);
}
}
Output:
csharpCopy code
[Banana, Apple, Orange]
We establish a HashSet named "fruits" in the example and add entries to it. The constructor of the "copySet" HashSet, which accepts a Collection as an input, is then used to generate a new HashSet. This constructor creates a new HashSet with the same elements as "fruits." The output shows the elements in the "copySet" in an unordered manner.
The HashSet class provides numerous ways to execute operations on the set. Among the often employed techniques are:
Boolean add(E e): On the off chance that the given component isn't, as of now, present, it is added to the set. On the off chance that the component was added, it returns valid; in any case, it gets back misleading.
Boolean remove(Object o): Assuming the provided component is available, the boolean remove(Object o) capability eliminates it from the set. In the event that the component is erased, the reaction is valid; in any case, it is bogus.
Boolean contains(Object o): contains(Object o) returns valid, assuming the mentioned component is available in the set and bogus in any case.
int size(): Returns the number of things in the set utilizing the int size() capability.
void clear(): Removes all set components.
Iterator<E> iterator(): Returns an iterator through the set's elements using the iteratorE> function.
Here's an example that demonstrates the usage of HashSet in Java:
javaCopy code
import java.util.HashSet;
public class HashSetExample {
public static void main(String[] args) {
HashSet<String> names = new HashSet<>();
names.add("John");
names.add("Alice");
names.add("Bob");
System.out.println("HashSet: " names);
System.out.println("Size: " names.size());
System.out.println("Contains 'Alice': " names.contains("Alice"));
names.remove("Bob");
System.out.println("After removal: " names);
}
}
Output:
yamlCopy code
HashSet: [Alice, Bob, John]
Size: 3
Contains 'Alice': true
After removal: [Alice, John]
We build a HashSet named "names" in this example and add three members to it. Afterward, we publish the HashSet and its size and determine whether or not it contains the element "Alice." After that, we remove the element "Bob" and print the updated HashSet.
As HashSet does not allow duplicate elements, any attempt to add duplicates is ignored. Here's an example:
javaCopy code
import java.util.HashSet;
public class HashSetDuplicatesExample {
public static void main(String[] args) {
HashSet<String> fruits = new HashSet<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Apple"); // Duplicate element
System.out.println(fruits);
}
}
Output:
csharpCopy code
[Orange, Banana, Apple]
In this example, we add the element "Apple" twice. However, HashSet ignores the duplicate element, and the output displays only one occurrence of "Apple" along with "Banana" and "Orange."
You can remove elements from a HashSet using the remove() method. Here's an example:
javaCopy code
import java.util.HashSet;
public class HashSetRemoveExample {
public static void main(String[] args) {
HashSet<String> colors = new HashSet<>();
colors.add("Red");
colors.add("Green");
colors.add("Blue");
System.out.println("Before removal: " colors);
colors.remove("Green");
System.out.println("After removal: " colors);
}
}
Output:
mathematicaCopy code
Before removal: [Green, Blue, Red]
After removal: [Blue, Red]
In this example, we create a HashSet called "colors" and add three elements to it. We then print the HashSet before removal. After that, we remove the element "Green" using the remove() method and print the updated HashSet.
You may create a HashSet from another collection using the constructor that accepts a Collection as input. Here's an illustration:
javaCopy code
import java.util.HashSet;
import java.util.ArrayList;
public class HashSetFromCollectionExample {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
HashSet<Integer> numberSet = new HashSet<>(numbers);
System.out.println("HashSet: " numberSet);
}
}
Output:
makefileCopy code
HashSet: [1, 2, 3]
In this model, three numbers are added to an ArrayList named "numbers" that was recently made. The constructor of a HashSet named "numberSet" that acknowledges an ArrayList as information is then used to create it. The components in the HashSet and the components in the ArrayList are shown in the result.
Let's consider an example where we use a HashSet to store a collection of Book objects. Each Book object is defined by its title and author. We can add books to the HashSet and perform operations like adding, removing, and checking for the existence of a specific book. Here's an example:
javaCopy code
import java.util.HashSet;
public class Book {
private String title;
private String author;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
@Override
public String toString() {
return title " by " author;
}
public static void main(String[] args) {
HashSet<Book> bookSet = new HashSet<>();
Book book1 = new Book("The Great Gatsby", "F. Scott Fitzgerald");
Book book2 = new Book("To Kill a Mockingbird", "Harper Lee");
Book book3 = new Book("1984", "George Orwell");
bookSet.add(book1);
bookSet.add(book2);
bookSet.add(book3);
System.out.println("HashSet: " bookSet);
System.out.println("Size: " bookSet.size());
Book bookToRemove = new Book("1984", "George Orwell");
bookSet.remove(bookToRemove);
System.out.println("After removal: " bookSet);
}
}
Output:
vbnetCopy code
HashSet: [To Kill a Mockingbird by Harper Lee, 1984 by George Orwell, The Great Gatsby by F. Scott Fitzgerald]
Size: 3
After removal: [To Kill a Mockingbird by Harper Lee, The Great Gatsby by F. Scott Fitzgerald]
In this example, a Book class is defined with characteristics for the author and title. To create a valid string representation of a Book object, we override the toString() function. We create three Book objects and add them to the HashSet called "bookSet." After printing the HashSet and its size, we create another Book object with the same title and author as "1984" and remove it from the HashSet using the remove() method. Finally, we print the updated HashSet.
Unique Elements: By forbidding duplication, HashSet makes sure that each element is distinct. The equals() function is used to look for duplication.
Fast Performance: HashSet provides constant-time performance for basic operations such as add, remove, and contains. This efficiency is achieved through the use of a hash table data structure.
Unordered Collection: HashSet does not maintain any specific order for its elements. The order of elements can change over time due to resizing or other internal operations.
To declare a HashSet variable, you can use the following syntax:
javaCopy code
HashSet<E> set = new HashSet<>();
Here, E represents the type of elements that the HashSet will hold. You can replace "E" with any valid Java class or interface.
HashSet utilizes a hash table information structure to store and recover passages. The hashCode() capability is utilized to decide a component's hash code when it is added to the HashSet. The component's stockpiling record in the hash is not set in stone by the hash code.
An impact happens when at least two passages share a similar hash code; in this situation, they are put away in a similar record utilizing a connected rundown. Once more, the hash code is determined while recovering a component, and the hash set looks for the component in the related record. Assuming that a connected rundown is available at that position, it crosses the rundown utilizing the equivalents() capability to find the matching component.
When to resize the internal hash table of a HashSet depends on its load factor. By default, the load factor is set to 0.75, which signifies that the HashSet will enlarge to accept more entries when it reaches 75% of its maximum capacity.
The official Java documentation has information about HashSet's implementation. The internal workings, constructors, methods, and use examples of the HashSet class are covered in the Java documentation.
The HashSet class provides several constructors to create instances of HashSet with different initial capacities and load factors. Some commonly used constructors are:
HashSet(): Constructs an empty HashSet with the initial default capacity (16) and load factor (0.75).
HashSet(Collection<? extends E> c): Makes a HashSet from the things of the given assortment utilizing the capability HashSet(Collection? extends E> c).
HashSet(int initialCapacity): Makes another unfilled HashSet with a default load element of 0.75 and the underlying limit provided.
HashSet(int initialCapacity, float loadFactor): A void HashSet with the provided beginning limit and burden factor is made by utilizing HashSet(int initialCapacity, float loadFactor).
You can add elements to a HashSet using the add() method. Here's an example:
javaCopy code
import java.util.HashSet;
public class HashSetAddExample {
public static void main(String[] args) {
HashSet<String> colors = new HashSet<>();
colors.add("Red");
colors.add("Green");
colors.add("Blue");
System.out.println(colors);
}
}
Output:
mathematicaCopy code
[Green, Blue, Red]
In this example, we create a HashSet called "colors" and add three elements to it using the add() method. The elements are printed in an unordered manner.
You can remove elements from a HashSet using the remove() method. Here's an example:
javaCopy code
import java.util.HashSet;
public class HashSetRemoveExample {
public static void main(String[] args) {
HashSet<String> colors = new HashSet<>();
colors.add("Red");
colors.add("Green");
colors.add("Blue");
System.out.println("Before removal: " colors);
colors.remove("Green");
System.out.println("After removal: " colors);
}
}
Output:
mathematicaCopy code
Before removal: [Green, Blue, Red]
After removal: [Blue, Red]
In this example, we create a HashSet called "colors" and add three elements to it. We then print the HashSet before removal. After that, we remove the element "Green" using the remove() method and print the updated HashSet.
You can iterate through the elements of a HashSet using an iterator or the enhanced for loop. Here's an example using the enhanced for loop:
javaCopy code
import java.util.HashSet;
public class HashSetIterationExample {
public static void main(String[] args) {
HashSet<String> colors = new HashSet<>();
colors.add("Red");
colors.add("Green");
colors.add("Blue");
for (String color : colors) {
System.out.println(color);
}
}
}
Output:
mathematicaCopy code
Green
Blue
Red
In this model, we make a HashSet called "varieties" and add three components to it. We utilize the improved circle to emphasize through the HashSet and print every component.
HashSet provides constant-time performance (O(1)) for basic operations such as add, remove, and contains. However, the performance can degrade under certain circumstances:
Hash Function Collisions: A linked list will store multiple elements with the same hash code in the same index. This can lead to performance degradation when the linked list becomes long.
Resizing: When a HashSet reaches its capacity threshold, it needs to be resized, which involves rehashing all the elements. This operation can be time-consuming for large HashSet instances.
Object Equality Checks: When checking for the existence of an element or removing an element, HashSet uses the equals() method. If the equals() method is computationally expensive, and it can impact performance.
Considering these factors when working with large HashSet instances or when dealing with complex hash functions and equality checks is important.
HashSet provides various methods to perform operations on the set. Some commonly used methods are:
equals(Object o): compares the provided item to the set to determine equality.
hashCode(): Provides the set's hash code value.
addAll(Collection<? extends E> c): Adds every element in the supplied collection to the set with the syntax addAll(Collection? extends E> c).
containsAll(Collection<?> c): The function containsAll(Collection? c) returns true if the set includes every member of the given collection.
HashSet can also be used with methods specified in interfaces java.util.Collection, java.lang.Iterable, and java.util.Set.
addAll(Collection<? expands E> c): Adds every one of the components in the predetermined assortment to the set.
containsAll(Collection<?> c): Returns valid, assuming the set contains every one of the components of the predetermined assortment.
iterator(): Returns an iterator over the components in the set.
addAll(Collection<? extends E> c): Adds all the elements in the specified collection to the set.
containsAll(Collection<?> c): Returns true if the set contains all the elements of the specified collection.
equals(Object o): Compares the specified object with the set for equality.
hashCode(): Returns the hash code value for the set.
HashSet in Java is a useful class that provides an implementation of the Set interface. It offers unique elements, fast performance, and data storage and retrieval flexibility. HashSet makes it simple to add, delete, and rapidly verify the presence of items. It is especially helpful when you need to keep a group of components without duplications. HashSet is a flexible data structure that may be used in a variety of contexts and applications where the need for uniqueness and speedy operations arises. By understanding its usage and capabilities, you can leverage HashSet effectively in your Java programs.
1. Can HashSet contain duplicate elements?
No, HashSet does not allow duplicate elements. If you try to add a duplicate element, it will be ignored.
2. Does HashSet maintain the insertion order of elements?
No, HashSet does not maintain any specific order for its elements. The order of elements can change over time due to resizing or other internal operations.
3. Can we store null values in a HashSet?
Yes, HashSet can store null values. It allows one null element.
4. How does HashSet handle collisions?
When multiple elements have the same hash code, HashSet stores them in the same index using a linked list. It traverses the linked list to find the matching element based on the equals() method.
5. Is HashSet thread-safe?
No, HashSet is not thread-safe. If multiple threads access a HashSet concurrently and at least one of them modifies the set, external synchronization is required to ensure thread safety.
PAVAN VADAPALLI
Director of Engineering
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working …Read More
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...