Sunday, April 13, 2014

How to get the key and values of various Map classes ?

import java.util.*;

public class MapKeysandValues {

@SuppressWarnings("unchecked")
public static void main(String[] args) {

Map<String, String> tm = new TreeMap();
tm.put("1", "one");
tm.put("2", "two");
tm.put("3", "three");
tm.put("4", "four");
System.out.println("TreeMap key values are " + tm.keySet());
System.out.println("TreeMap values are " + tm.values());

}

}

Result Is : 
TreeMap key values are [1, 2, 3, 4]
TreeMap values are [one, two, three, four]

The same applies for all the Map classes, like HashMap, HashTable etc.

No comments:

Post a Comment