import java.util.*; public class TreeMapDemo { public static void main(String[] args) { TreeMap<String, Integer> tm = new TreeMap<>(); tm.put("Anand", 20); tm.put("Mary", 20); tm.put("Ravi", 21); tm.put("John",19); System.out.println("Contents: "+tm); System.out.println(); Set<String> keys = tm.keySet(); for(String k: keys) System.out.println(k+" -- "+tm.get(k)); System.out.println(); Set<Map.Entry<String, Integer>> entries = tm.entrySet(); for(Map.Entry<String,Integer> each: entries) System.out.println(each.getKey()+" -- "+each.getValue()); System.out.println(); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter