Saturday, January 16, 2016

HashSet In Java With Examples

Set:

Set is an interface which extends Collection interface.This can be used for representing a group of individual objects where it does not allow duplicate elements and insertion order is not preserved that means no guarantee in the order .That means it stores only unique elements.set interface is child interface of Collection.Set does not contains any special methods  and we have to use only Collection interface methods. Set allows at most one null element

HashSet:

HashSet is a class that implementation of Set interface.It does not allow duplicate objects  and insertion order is not preserved and it is based on hashcode of  the object.
The underlying data structure for HashSet is Hash Table. Here Hash Table stores the information in the mechanism of hashing. HashSet allows one null element. So null insertion is possible. Heterogeneous objects are allowed. HashSet is best choice if frequent operation is Search Operation. HashSet does not support synchronized . so it does not guarantee the order will remain which the order you insert.





Constructors:

1. HashSet hs=new HashSet();
//Here creates an Empty HashSet Object which default initial value is 16 and default fill ratio is 0.75

2. HashSet hs=new HashSet(int initialCapacity);
3. HashSet hs = new HashSet(int initialCapacity, float fillratio)
Here fillratio is 0 or 1.
4. HashSet h = new HashSet(Collection c)




Methods in HashSet:

HashSet inherits  all the methods of Collections and Set interfaces.But one method is added extra to this class that is given below
Object clone();//this method returns cloned hashset object having same elements of original hashset.

Example:

import java.util.*;
public class HashSetDemo{
public static void main(String args[])
{
HashSet hs=new HashSet();
hs.add("lucky");
hs.add("sree");
hs.add("raju");
hs.add("nani");
hs.add(null);
hs.add(new Integer(10));
System.out.println(hs);
}
}

Output :


Example 2: If you add duplicate elements to the HashSet then see the output


import java.util.*;
public class HashSetDemo{
public static void main(String args[])
{
HashSet hs=new HashSet();
hs.add("lucky");
hs.add("sree");
hs.add("raju");
hs.add("nani");
hs.add("lucky");
hs.add(null);
hs.add(new Integer(10));
System.out.println(hs);
}
}

Output:




Linked HashSet:

This is child class of HashSet.It is similar to HashSet but the main difference between HashSet and Linked HashSet are 

                      HashSet                                                Linked HashSet
1.The underlying data structure                      1. The underlying Data Structure is Hash Table and is HashTable                                                      Linked List

2.Insertion order is not preserved                   2. Insertion Order is Preserved

3.It is introduced in 1.2 version                      3. It is introduced in 1.4 version  



Example:

import java.util.*;
public class LinkedHashSetDemo{
public static void main(String args[])
{
LinkedHashSet hs=new LinkedHashSet();
hs.add("lucky");
hs.add("sree");
hs.add("raju");
hs.add("nani");
hs.add(null);
hs.add(new Integer(10));
System.out.println(hs);
}
}

Output :



When do you use LinkedHashSet:

When you are looking a copy of set that has the same order as the Original.That means what ever you give order of the values as  input the same input value will come as result as in the same order.


Is HashSet Synchronized?

HashSet implementation is not synchronized. If multiple threads access a set concurrently at least one of the thread modifies as set,it must be modifies externally.

Example:

Set s=Collections.SynchronizedSet(new HashSet(...));





















No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...