java.util(二)HashMap TreeMap LinkedHashMap

import java.util.*;

public class TestMap {
  public static void main(String[] args) {
    // Create a HashMap
    Map<String, Integer> hashMap = new HashMap<String, Integer>();
    hashMap.put("Smith", 30);
    hashMap.put("Anderson", 31);
    hashMap.put("Lewis", 29);
    hashMap.put("Cook", 29);

    System.out.println("Display entries in HashMap");
    System.out.println(hashMap);

    // Create a TreeMap from the previous HashMap
    Map<String, Integer> treeMap = 
      new TreeMap<String, Integer>(hashMap);
    System.out.println("\nDisplay entries in ascending order of key");
    System.out.println(treeMap);

    // Create a LinkedHashMap
    Map<String, Integer> linkedHashMap =
      new LinkedHashMap<String, Integer>(16, 0.75f, true);
    linkedHashMap.put("Smith", 30);
    linkedHashMap.put("Anderson", 31);
    linkedHashMap.put("Lewis", 29);
    linkedHashMap.put("Cook", 29);

    // Display the age for Lewis
    System.out.println("The age for " + "Lewis is " +
      linkedHashMap.get("Lewis").intValue());
    System.out.println("The age for " + "Lewis is " +
      linkedHashMap.get("Smith").intValue());
    System.out.println("\nDisplay entries in LinkedHashMap");
    System.out.println(linkedHashMap);
  }
}


發佈了47 篇原創文章 · 獲贊 18 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章