share

Friday, 19 June 2015

How to get entry set from TreeMap

import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

public class TreeMapEntrySet {
   
    public static void main(String[] args) {
       
        Map<Integer, String> tm = new TreeMap<Integer,String>();
       
        // add key and values
        tm.put(1,"java");
        tm.put(2,"cpp");
        tm.put(3,"php");
        tm.put(4,"c");
        tm.put(5, "mysql");
       
        //print treemap
   
        System.out.println("TreeMap Is="+tm);
       
        //Returns a Set view of the mappings contained in this
        // map
        Set<Entry<Integer, String>> entry =tm.entrySet();
       
        for (Entry<Integer, String> entry2 : entry) {
            System.out.println("key ="+entry2.getKey()+" value ="+entry2.getValue());
        }
       
    }

}




Output:


TreeMap Is={1=java, 2=cpp, 3=php, 4=c, 5=mysql}

key =1 value =java

key =2 value =cpp

key =3 value =php

key =4 value =c

key =5 value =mysql

No comments:

Post a Comment