Monday 15 May 2017

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


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

GenericDemo.java
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/*
 * Example of Java Generics using Map 
 */
public class GenericDemo
{
    public static void main(String[] args)
    {
        Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(1, "Peter");
        map.put(2, "Stephan");
        map.put(3, "Sara");

        Set<Map.Entry<Integer, String>> set = map.entrySet();

        Iterator<Map.Entry<Integer, String>> itr = set.iterator();
        while (itr.hasNext())
        {
            // no need to typecast
            Map.Entry<Integer, String> e = itr.next();
            System.out.println(e.getKey() + " " + e.getValue());
        }
    }
}
Output
1 Peter
2 Stephan
3 Sara
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/GenericsDemo_Map_App.zip?attredirects=0&d=1

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/47d09ebb24294f7b507419345f38ec4cde27d544/BasicJava/GenericsDemo_Map_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