Friday, 21 October 2016

What is HashSet In Java ?

Previously we have discussed about
Here we learn what is HashSet In Java.But before that we must know, What is Collection Interface in Java ?
Class HashSet<E>

Type Parameters:
E - the type of elements maintained by this set

All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, Set<E>

Direct Known Subclasses:
JobStateReasons, LinkedHashSet

Declaration of HashSet:
public class HashSet<E>
extends AbstractSet<E>
implements Set<E>, Cloneable, Serializable

HashSet extends the AbstractSet and implements the Set interface, it uses HashTable for storage.HashSet doesn't guarantees the order of Set, means it does not guarantee that the order will remain constant over time. HashSet allows the null element.

HashSet class offers constant time performance for the basic operations (add, remove, contains and size), it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.

HashSet implementation is not synchronized, means if multiple threads access a hash set concurrently, and at least one of the threads modifies the set, it must be synchronized externally, to prevent accidental unsynchronized access to the set:
Set s = Collections.synchronizedSet(new HashSet(...));

HashSet is an example of fail-fast, means if the set is modified at any time after the iterator is created, in any way except through the iterator's own remove method, the Iterator throws a ConcurrentModificationException.
This class is a member of the Java Collections Framework.

Constructor Summary of HashSet:

Constructor
Description
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.

Method Summary of HashSet:

Modifier and Method name
Description
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).

Example of HashSet:

import java.util.HashSet;
import java.util.Iterator;

public class HashSetClassExample {

public static void main(String[] args) {
HashSet<String> hs = new HashSet<String>();
hs.add("apple");
hs.add("banana");
hs.add("orange");
hs.add("guava");
System.out.println("Size of HashSet : "+hs.size());
System.out.println("HashSet Elements : "+hs);
System.out.println("\nIteration over HashSet using Iterator: ");
Iterator<String> it = hs.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
System.out.println("\nIteration over HashSet Using Lambda Expression : ");
hs.forEach( st -> System.out.println(st));
}

}
Program Output :

Size of HashSet : 4
HashSet Elements : [banana, orange, apple, guava]

Iteration over HashSet using Iterator: 
banana
orange
apple
guava

Iteration over HashSet Using Lambda Expression : 
banana
orange
apple
guava


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



No comments:

Post a Comment