In this chapter you will learn:
- What is HashSet Class in Java?
- Declaration and Methods of HashSet Class
- Programming Example
What is HashSet class in Java?
The HashSet Class implements the Set interface and stored items into a hash table. Iteration order might be different from insertion order. That means it takes no guarantee of element’s order as they inserted or no guarantee of same order result in each iteration. Some Fact about HashSet Class:
- It stores data in a Hash Table.
- Orders of elements are not guaranteed and you may get different order result in each iteration.
- It allows null elements.
- HashSet is not synchronized so if multiple threads are accessing this and one thread makes changes then you must synchronize HashSet Externally.
Constructors
Constructors | Descriptions |
HashSet() |
Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75). |
HashSet(Collection<? extends E> c) |
Constructs a new set containing the elements in the specified collection. |
HashSet(int initialCapacity) |
Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75). |
HashSet(int initialCapacity, float loadFactor) |
Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and the specified load factor. |
Methods
Methods | Descriptions |
boolean add(E e) |
Adds the specified element to this set if it is not already present. |
void clear() |
Removes all of the elements from this set. |
Object clone() |
Returns a shallow copy of this HashSet instance: the elements themselves are not cloned. |
boolean contains(Object o) |
Returns true if this set contains the specified element. |
boolean isEmpty() |
Returns true if this set contains no elements. |
Iterator<E> iterator() |
Returns an iterator over the elements in this set. |
boolean remove(Object o) |
Removes the specified element from this set if it is present. |
int size() |
Returns the number of elements in this set (its cardinality). |
Methods Inherited From Class
Class | Methods |
java.util.AbstractSet |
equals(), hashCode(), removeAll() |
java.util.AbstractCollection |
addAll(), containsAll(), retainAll(), toArray(), toArray(), toString() |
java.lang.Object |
finalize(), getClass(), notify(), notifyAll(), wait(), wait(), wait() |
java.util.Set |
addAll(), containsAll(), equals(), hashCode(), removeAll(), retainAll(), toArray(), toArray() |
Declaration:
HashSeth=new HashSet ();
Programming Example
import java.util.*; class HashSet_Example { public static void main(String[] args) { HashSet; h=new HashSet (); //Adding items in Hash Table h.add("PHP"); h.add("Java"); h.add("HTML"); h.add("SQL"); h.add("C#"); //printing HashTable Iterator itr=h.iterator(); while(itr.hasNext()) { System.out.print(itr.next() + " "); } //Removing item from HashSet System.out.println("\nItem Removed : "+h.remove("Java")); //Printing HashTable System.out.println(h); } }
Output
D:\JavaProgram>javac HashSet_Example.java
D:\JavaProgram>java HashSet_ExampleSQL C# PHP HTML Java
Item Removed : true
[SQL, C#, PHP, HTML]
_
D:\JavaProgram>java HashSet_ExampleSQL C# PHP HTML Java
Item Removed : true
[SQL, C#, PHP, HTML]
_
Summary
In this chapter you have learned HashSet Class in Java with complete programming example. In the next chapter you will learn about LinkedHashSet Class.