Friday, 6 January 2017

HashSet in java

HashSet implements Set interface and extends AbstractSet class. It is part of java.util package.

Key feature of HashSet:

1.       HashSet contain unique elements only. If you add duplicate in it, previous value will be overwritten (see example).
2.       HashSet allows one null value only.
3.       HashSet doesn’t maintain insertion order so when you retrieve elements from it they may be returned in random order. If you want to get element in order of insertion, use LinkedHashSet.
4.       HashSet is non-Synchronized.
5.       HashSet internally uses HashTable to store the elements.

Difference

·         HashSet vs LinkedHashSet
·         HashSet vs TreeSet
·         ArrayList vs HashSet
Now let’s look at the ways in which you can create HashSet in java. Please click on each to learn the creation technique.

·         HashSet hSet = new HashSet();
·         HashSet hSet = new HashSet (Collection c);
·         HashSet hSet = new HashSet (int initialCapacity);
·         HashSet hSet = new HashSet (int initialCapacity, float LoadFactor);
Now let’s look at different ways in which you can use HashSet that we created.

HashSet operations

·         Sort the elements in HashSet (if sorting is required, better use TreeSet)
·         Search element in HashSet
·         Remove element from HashSet
·         Remove all elements from the HashSet
·         Remove element from a particular index in HashSet

Traverse LinkedList

·         Traverse HashSet using Iterator
·         Traverse HashSet using Enumeration

NoteYou cannot traverse Set with ListIterator

Monday, 2 January 2017

Java Difference : Iterator vs Enumeration

Iterator and Enumeration differ based on the following parameters:

1.      Introduced
Enumeration was introduced as part of JDK1.0
Iterator was introduced from JDK1.2

2.      Modifying Collection
Enumeration cannot be used to modify the elements in Collection while traversing.
Iterator can be used to remove the element in Collection while traversing (it can remove element using remove() )

3.      Available methods
Enumeration:  hasMoreElements(), nextElement()
Iterator:  hasNext(), next(), remove()

4.      Fail fast or Fail safe
Enumeration is fail safe by nature.
Iterator is fail fast by nature.

Note : Iterator being fail fast by nature is considered safe than enumeration as it throws ConcurrentModificationException, if any other thread tries to modify the Collection that is being traversed by Iterator.

5.      Legacy concept
Enumeration is generally preferred for traversing over legacy classes like Vector, HashTable, etc.
Iterator is used to traverse over almost all classes in java Collection framework like ArrayList, LinkedList, HashMap, HashSet, etc.

Note: Iterator is preferred over Enumeration. 



Example: 

package Traversal;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class TraversalMethods {

      public static void main(String[] args) {
            List<String> arrayList = new ArrayList<String>();
            arrayList.add("Rajneesh");
            arrayList.add("Shweta");
            arrayList.add("Anup");

            Iterator<String> listIterator = arrayList.iterator();
            System.out.println("Traversing list using iterator");

            while(listIterator.hasNext()){

                  String token = (String)listIterator.next();
                  System.out.println(token);
                  if(token.equals("Shweta")){
                        listIterator.remove();   //iterator remove() method facility
                  }
            }
            System.out.println("ArrayList after removal operation");
            System.out.println(arrayList);

            Vector<String> vector = new Vector<String>();
            vector.add("Puja");
            vector.add("Mukesh");
            vector.add("Titli");

            System.out.println("Traversing vector using Enumeration");
            Enumeration<String> enumeration = vector.elements();

            while(enumeration.hasMoreElements()){

                  System.out.println(enumeration.nextElement());
                  //enumeration.remove(); METHOD DOESNOT EXIST
            }

      }

}


In the given example you can see iterator provides you with remove() method whereas in enumeration no such method exists.

Hope this helps. IF there is any query then put in the comment box and we can discuss it and clarify.

You may also like to read:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.