Hello guys, we meet again for our journey of learning Java. Today, we are gonna learn something very fruitful and easy. Hope you all are excited and ready. I would recommend you guys to go through the HashMap or TreeMap article which goes over hashing as this topic has some of its features. So, What's the wait? Let's start! Suppose you guys are given the task of storing car names (yes, I love cars :p). Now, how would you store it? If you guys have some questions, here are the requirements. The car names may increase over time. So, logically, an array is not what we are looking for.
So, you guys must be thinking that we can store them in an ArrayList. yes, that's true. But, as we know there can only be 1 car name associated with 1 car, we know that all names are gonna be unique. Can we use this information to decrease the time complexity of searching an ArrayList?
What is HashSet in Java?
Code:
import java.util.HashSet;
import java.util.Set;
public class MyHashSet {
public static void main(String[] args) {
Set<String> myCarSet = new HashSet<>();
myCarSet.add("BMW M3");
myCarSet.add("Audi A8");
myCarSet.add("Mercedes Q3");
myCarSet.add("Bugatti veyron");
myCarSet.add("Ferrari spider");
myCarSet.forEach(a -> System.out.print(a+" "));
System.out.println("Traversal completed!");
if(myCarSet.contains("BMW M3")) System.out.println("Yes! BMW hai");
myCarSet.remove("BMW M3");
System.out.println("Traversal started!");
myCarSet.forEach(a -> System.out.print(a + " "));
System.out.println("Traversal completed!");
}
}
Output:
Now, that you guys have seen the implementation, you can see yourself as how much fast, efficient and easy it is to use HashSet. Let's see some of the important points of Hashset.Internal workings of a HashSet:
Map backs up all Set interface classes internally. Internally, HashSet stores its object using HashMap. You might be asking why, in HashMap, we require a key-value pair to input a value, while in HashSet, we just pass one value.In a HashMap, the value we put in the HashSet works as a key to the map Object, and java utilises a constant variable to store the value. As a result, all of the values in a key-value pair will be the same.HashSet uses the hashCode() and equals() methods to verify if there is an existing entry before saving an Object. Two lists are deemed equal in the preceding example if they have the same entries in the same order. Because the two lists are equal, they will both return the same hash when you use the hashCode() function on them.HashSet does not keep duplicate items; if you give it two identical objects, it will only store the first one. HashSet operations are time-consuming because the underlying data structure is a hashtable. As a result, average or common case time complexity for HashSet operations like add, remove, and look-up (contains method) is O(1).That's all about What is HashSet in Java and how to use HashSt in Java. Hashset is an important class of Java Collection framework and very useful if you want to store unique elements or want to avoid duplicates. As I have explained, HashSet is backed by HashMap which is an hash table, hence it provides O(1) performance for adding and retriving objects.Other Java Collection Articles you may like
- 21 skills Java Programmer Should Learn
- How to use WeakHashMap in Java
- 10 Frameworks Java developers should learn
- How to compare objects by multiple fields
- Difference between HashMap and ArrayList in Java
- Comparator Comparing and thenComparing example
- 7 Best Courses to learn Java Collections and Stream API
- When to use Map, List, and Set collection in Java
- 10 Advanced Core Java Courses for Programmers
- Difference between HashMap and HashSet in Java
- 8 Best Java Functional Programming Courses
- Difference between IdentityHashMap and HashMap in Java
- 50+ Java Collection Interview Questions with Answers
- Top 5 Courses to become a Fullstack Java Developer
Thanks for reading this article if you find these Java HahsSet examples useful then please share them with your friends and colleagues. If you have any questions or feedback then please drop me a note.P. S. - If you are new to Java and looking for a free course to learn Java in a structured way then you can also check this Java Tutorial for Complete Beginners(FREE) course on Udemy. It's completely free and more than 1.4 million developers have joined this course. You just need a free Udemy account to join the course.
No comments:
Post a Comment
Feel free to comment, ask questions if you have any doubt.