This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element. This class is not synchronized. However it can be synchronized explicitly like this: Set s = Collections.synchronizedSet(new HashSet(...));
Points to Note about HashSet:
- HashSet doesn’t maintain any order, the elements would be returned in any random order.
- HashSet doesn’t allow duplicates. If you try to add a duplicate element in HashSet, the old value would be overwritten.
- HashSet allows null values however if you insert more than one nulls it would still return only one null value.
- HashSet is non-synchronized.
- The iterator returned by this class is fail-fast which means iterator would throw
ConcurrentModificationException
if HashSet has been modified after creation of iterator, by any means except iterator’s own remove method.
HashSet Example
import java.util.HashSet; public class HashSetExample { public static void main(String args[]) { // HashSet declaration HashSet<String> hset = new HashSet<String>(); // Adding elements to the HashSet hset.add("Apple"); hset.add("Mango"); hset.add("Grapes"); hset.add("Orange"); hset.add("Fig"); //Addition of duplicate elements hset.add("Apple"); hset.add("Mango"); //Addition of null values hset.add(null); hset.add(null); //Displaying HashSet elements System.out.println(hset); } }
Output:
[null, Mango, Grapes, Apple, Orange, Fig]
As you can see there all the duplicate values are not present in the output including the duplicate null value.
HashSet tutorials
- Delete all elements from HashSet
- How to iterate through a HashSet
- Convert a HashSet to an array
- Convert a HashSet to a TreeSet
- Convert HashSet to a List/ArrayList
- HashSet vs HashMap
- HashSet vs TreeSet
HashSet Methods:
- boolean add(Element e): It adds the element e to the list.
- void clear(): It removes all the elements from the list.
- Object clone(): This method returns a shallow copy of the HashSet.
- boolean contains(Object o): It checks whether the specified Object o is present in the list or not. If the object has been found it returns true else false.
- boolean isEmpty(): Returns true if there is no element present in the Set.
- int size(): It gives the number of elements of a Set.
- boolean(Object o): It removes the specified Object o from the Set.
What is the difference between HashSet & ArrayList.
Array list is ordered collection and allowed duplicates where HashSet is not
In Array List Dont follow default Sorting mechanism but In case Of Hash Set its possible and Insertion is not preserved in Hash set
So the Collection Gives Facility for default sorting by using utility class ‘Collections’
quick and easy tutorial for java hashsets. thanks!
I think this point is not correct:
2.-HashSet doesn’t allow duplicates. If you try to add a duplicate element in HashSet, the old value would be overwritten.
The Java docs mention: If this set already contains the element, the call leaves the set unchanged and returns false.
Hash set can contains null value whereas array list cannot
hello
thanks for your site
i have a Question
how can
Writing program with Hashset that show me
Repetitive words ???
This tutorial is superb, I like it.
and It think by – “if HashSet has been modified after creation of iterator”
You mean this:
HashSet hs = new HashSet();
Song s1 = new Song(“zzz”, “zaheer”, “23”, “343”);
Song s2 = new Song(“xx”, “mosin”, “23”, “343”);
hs.add(s1);
hs.add(s2);
Iterator itr = hs.iterator();
Song s6 = new Song(“ooo”, “yakoob”, “23”, “343”);
hs.add(s6);
while (itr.hasNext()) {
System.out.println(itr.next());
}
==================Hash List===================
[null, Apple, Grapes, Fig, Mango, Orange]
hi, I tried your code but it gets the result in sorted format.
can equal objects be added to hashset ? if yes then why it is not considered as duplicate ?
thank you for the tutorial
but the fact that ” If you try to add a duplicate element in HashSet, the old value would be overwritten.” is not true.
according to what it says on oracle website in this link (https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html), it’s the other way around (i.e. if element_to_be_added is a duplicate, it won’t be added at all):
public boolean add(E e)
Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if this set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false.