Category: HashSet
How to create and add elements in a HashSet in Java
+In this article we will discuss how to create a HashSet of integers and add elements in it. Creating a HashSet Object for integer // Create a HashSet of int HashSet<Integer> setOfInts = new HashSet<>(); This hashset object can contain only unique integers. Advertisements Adding Element in a HashSet To add an element in a …
How to create and add elements in a HashSet in JavaRead More
What is Hashing and Hash Table?
What is Hashing ? Hashing is a technique used to store elements in such a way that searching over them should be very fast. It internally uses a Hash Table to store the elements. What is Hash Table ? Hash Table is a kind of Data Structure Used to achieve Hashing. It internally maintains a …
Initializing a HashSet from an Array or a Collection
In this Article we will discuss how to initialize a HashSet using a Array or a Collection. HashSet has a Constructor that receives a Collection as an argument and initializes the new Set with the elements of passed collection object. Advertisements public HashSet(Collection<? extends E> c) Initializing a HashSet with an array Suppose we need to …
Initializing a HashSet from an Array or a CollectionRead More
How to convert a HashSet into an array in Java
In this article we will discuss how to convert a HashSet in to an array. Each collection that provides a member function toArray() i.e. Advertisements public <T> T[] toArray(T[] a) It iterates through all the elements of Collection (HashSet in our case) and set the each elements in the passed argument array []a. But if array …
How to Merge two HashSets in Java
In this article we will discuss how to merge two HashSets. HashSet provides a member function addAll() i.e. Advertisements public boolean addAll(Collection<? extends E> c) It provides a functionality to add a collection into to a HashSet. But this Collection should be of Same Type as of HashSet or of its Base Class. How addAll() works It …
How to Merge an Array in a HashSet in Java
In this article we will discuss how to merge an array in a HashSet HashSet has a member function addAll() that can add a collection into to a HashSet. But this Collection should be of Same Type as of HashSet or of its Base Class. Advertisements public boolean addAll(Collection<? extends E> c)  It will add the each …
How to Search for an element in HashSet in Java
In this article we will discuss how to search for an element in HashSet. Main feature of a HashSet is Fast searching. Which it achieves using Hashing. Now suppose we want to search if an element exists in HashSet ? Advertisements It can be done using contains() member function of HashSet i.e. // It checks if the given …
How to iterate over a HashSet in Java
In this article we will discuss how to Iterate over a HashSet in Java using HashSet. Fetch Iterator from HashSet To iterate over a HashSet we will use an Underlying Iterator object of the HashSet. Advertisements Fetch the Iterator from a HashSet object use its member function iterator() i.e. // Create a HashSet of String HashSet<String> …