Monday 22 December 2014

Java : Collection Framework : LinkedHashSet (Iterator)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=OMao0YxxTwg&list=UUhwKlOVR041tngjerWxVccw

LinkedHashSetExample.java
import java.util.LinkedHashSet;
import java.util.Iterator;

/*
 * Example of iterator() method.
 */
public class LinkedHashSetExample
{
    public static void main( String[] args )
    {
        LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>();

        linkedHashSet.add("Dave");
        linkedHashSet.add("Peter");
        linkedHashSet.add("Phil");
        linkedHashSet.add("Rohit");
        linkedHashSet.add("Virat");

        System.out.println("linkedHashSet : " + linkedHashSet + "\n");

        /*
         * Returns an iterator over the elements in this set. The elements are
         * returned in no particular order (unless this set is an instance of
         * some class that provides a guarantee).
         */

        Iterator<String> iterator = linkedHashSet.iterator();

        /*
         * Using Iterator to get each element from the LinkedHashSet.
         */

        while( iterator.hasNext() )
        {
            String name = iterator.next();
            System.out.println(name);
        }

    }
}

Output
linkedHashSet : [Dave, Peter, Phil, Rohit, Virat]

Dave
Peter
Phil
Rohit
Virat
To Download LinkedHashSetDemoIterator Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/LinkedHashSetDemoIterator.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • No comments:

    Post a Comment