Lecture Videos
  1  import java.util.*;
  2  
  3  public class TestMap {
  4    public static void main(String[] args) {
  5      // Create a HashMap
  6      Map<String, Integer> hashMap = new HashMap<>();
  7      hashMap.put("Perez", 30);
  8      hashMap.put("Washington", 31);
  9      hashMap.put("Lewis", 29);
 10      hashMap.put("Cook", 29);
 11  
 12      System.out.println("Display entries in HashMap");
 13      System.out.println(hashMap + "\n");
 14  
 15      // Create a TreeMap from the preceding HashMap
 16      Map<String, Integer> treeMap = new TreeMap<>(hashMap);
 17      System.out.println("Display entries in ascending order of key");
 18      System.out.println(treeMap);
 19  
 20      // Create a LinkedHashMap
 21      Map<String, Integer> linkedHashMap =
 22        new LinkedHashMap<>(16, 0.75f, true);
 23      linkedHashMap.put("Perez", 30);
 24      linkedHashMap.put("Washington", 31);
 25      linkedHashMap.put("Lewis", 29);
 26      linkedHashMap.put("Cook", 29);
 27  
 28      // Display the age for Lewis
 29      System.out.println("\nThe age for " + "Lewis is " +
 30        linkedHashMap.get("Lewis"));
 31  
 32      System.out.println("Display entries in LinkedHashMap");
 33      System.out.println(linkedHashMap);
 34      
 35      // Display each entry with name and age
 36      System.out.print("\nNames and ages are ");
 37      treeMap.forEach(
 38        (name, age) -> System.out.print(name + ": " + age + " "));
 39    }
 40  }