Friday, 9 June 2017

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


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:

CLICK HERE

No comments:

Post a Comment