TreeMap in Java
Advertisements
TreeMap in Java
A TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class.
Points to Remember
- It contains only unique elements.
- It cannot have null key but can have multiple null values.
- It is same as HashMap instead maintains ascending order.
Example of TreeSet
import java.util.*; class TreeMapDemo { public static void main(String args[]) { TreeMap<Integer,String> tm=new TreeMap<Integer,String>(); tm.put(1,"Deo"); tm.put(2,"zen"); tm.put(3,"porter"); tm.put(4,"piter"); for(Map.Entry m:tm.entrySet()) { System.out.println(m.getKey()+" "+m.getValue()); } } }
Output
1,Deo 2,zen 3,porter 4, piter
Google Advertisment