Tuesday, 16 December 2014

Java : Collection Framework : HashSet (ToArray)


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

HashSetExample.java
import java.util.HashSet;

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

        hashSet.add("Rohan");
        hashSet.add("Phil");
        hashSet.add("Ram");
        hashSet.add("Dave");
        hashSet.add("Peter");

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

        /*
         * Returns an array containing all of the elements in this set. 
         * 
         * This method acts as bridge between array-based and collection-based APIs.
         */

        Object[] objArray = hashSet.toArray();

        for( Object object : objArray )
        {
            String name = (String) object;
            System.out.println(name);
        }

    }
}

Output
  

hashSet : [Rohan, Dave, Phil, Peter, Ram]

Rohan
Dave
Phil
Peter
Ram


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

    Google+ Followers

    Google+ Badge