Monday 15 May 2017

Java Tutorial: Generics in java | Java Generics [Generics example using HashSet]


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

GenericDemo.java
import java.util.HashSet;
import java.util.Iterator;

public class GenericDemo
{
    public static void main(String[] args)
    {
        HashSet<String> nameSet = new HashSet<String>();
        nameSet.add("Peter");
        nameSet.add("John");
        nameSet.add("Juli");

        System.out.println("----------Using for-each loop---------------");
        /*
         * Using for-each loop we can loop and get each name from nameSet
         */
        for (String name : nameSet)
        {
            System.out.println(name);
        }

        System.out.println("----------Using Iterator---------------------");
        Iterator<String> iterator = nameSet.iterator();

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

    }

}
Output
----------Using for-each loop---------------
John
Peter
Juli
----------Using Iterator---------------------
John
Peter
Juli
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/GenericsDemo_HashSet_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/GenericsDemo_HashSet_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/47d09ebb24294f7b507419345f38ec4cde27d544/BasicJava/GenericsDemo_HashSet_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment