import java.util.HashMap; import java.util.LinkedHashMap; import java.util.TreeMap; import java.util.Map; public class CollectionDemo { public static void main(String[] args) { // HashMap demonstration System.out.println("HashMap:"); Map<String, Integer> hashMap = new HashMap<>(); hashMap.put("Alice", 25); hashMap.put("Bob", 30); hashMap.put("Charlie", 35); for (Map.Entry<String, Integer> entry : hashMap.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } // LinkedHashMap demonstration System.out.println("\nLinkedHashMap:"); Map<String, Integer> linkedHashMap = new LinkedHashMap<>(); linkedHashMap.put("Alice", 25); linkedHashMap.put("Bob", 30); linkedHashMap.put("Charlie", 35); for (Map.Entry<String, Integer> entry : linkedHashMap.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } // TreeMap demonstration System.out.println("\nTreeMap:"); Map<String, Integer> treeMap = new TreeMap<>(); treeMap.put("Alice", 25); treeMap.put("Bob", 30); treeMap.put("Charlie", 35); for (Map.Entry<String, Integer> entry : treeMap.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } } }
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