Monday 15 December 2014

Java : Collection Framework : HashSet (Iterator)


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

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

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

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

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

        /*
         * Returns an iterator over the elements in this set. The elements are
         * returned in no particular order.
         */

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

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

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

    }
}

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

Rohit
Dave
Phil
Peter
Virat

To Download HashSetDemoIterator Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/HashSetDemoIterator.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