public
TreeMap()
Constructs
a new, empty tree map, using the natural ordering of its keys. All
the keys inserted into the TreeMap must implements the Comparable
interface.
import java.util.*; class TreeMapConstructor1{ public static void main(String args[]){ TreeMap<Integer, String> myMap; myMap = new TreeMap<> (); /* Insert data to myMap */ myMap.put(1, "a"); myMap.put(7, "d"); myMap.put(2, "b"); myMap.put(6, "d"); myMap.put(3, "c"); myMap.put(5, "d"); myMap.put(4, "d"); System.out.println("Elements in myMap are"); System.out.println(myMap); } }
Output
Elements in myMap are {1=a, 2=b, 3=c, 4=d, 5=d, 6=d, 7=d}
As
you observe the output, TreeMap prints the output in natural ordering
of the key, which is integer here.
No comments:
Post a Comment