程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 使用Maps

使用Maps

編輯:關於JAVA

Map(接口) 維持“鍵-值”對應關系(對),以便通過一個鍵查找相應的值
HashMap* 基於一個散列表實現(用它代替Hashtable)。針對“鍵-值”對的插入和檢索,這種形式具有最穩定的性能。可通過構建器對這一性能進行調整,以便設置散列表的“能力”和“裝載因子”
ArrayMap 由一個ArrayList後推得到的Map。對反復的順序提供了精確的控制。面向非常小的Map設計,特別是那些需要經常創建和刪除的。對於非常小的Map,創建和反復所付出的代價要比HashMap低得多。但在Map變大以後,性能也會相應地大幅度降低
TreeMap 在一個“紅-黑”樹的基礎上實現。查看鍵或者“鍵-值”對時,它們會按固定的順序排列(取決於Comparable或Comparator,稍後即會講到)。TreeMap最大的好處就是我們得到的是已排好序的結果。TreeMap是含有subMap()方法的唯一一種Map,利用它可以返回樹的一部分。

Map (interface)
 

Maintains key-value associations (pairs), so you can look up a value using a key.
 

HashMap*
 

Implementation based on a hash table. (Use this instead of Hashtable.) Provides constant-time performance for inserting and locating pairs. Performance can be adjusted via constructors that allow you to set the capacity and load factor of the hash table.
 

TreeMap
 

Implementation based on a red-black tree. When you view the keys or the pairs, they will be in sorted order (determined by Comparable or Comparator, discussed later). The point of a TreeMap is that you get the results in sorted order. TreeMap is the only Map with the subMap() method, which allows you to return a portion of the tree.
 

下例包含了兩套測試數據以及一個fill()方法,利用該方法可以用任何兩維數組(由Object構成)填充任何Map。這些工具也會在其他Map例子中用到。
 

//: Map1.java
// Things you can do with Maps
package c08.newcollections;
import java.util.*;

public class Map1 {
  public final static String[][] testData1 = {
    { "Happy", "Cheerful disposition" },
    { "Sleepy", "Prefers dark, quiet places" },
    { "Grumpy", "Needs to work on attitude" },
    { "Doc", "Fantasizes about advanced degree"},
    { "Dopey", "'A' for effort" },
    { "Sneezy", "Struggles with allergies" },
    { "Bashful", "Needs self-esteem workshop"},
  };
  public final static String[][] testData2 = {
    { "Belligerent", "Disruptive influence" },
    { "Lazy", "Motivational problems" },
    { "Comatose", "Excellent behavior" }
  };
  public static Map fill(Map m, Object[][] o) {
    for(int i = 0; i < o.length; i++)
      m.put(o[i][0], o[i][1]);
    return m;
  }
  // Producing a Set of the keys:
  public static void printKeys(Map m) {
    System.out.print("Size = " + m.size() +", ");
    System.out.print("Keys: ");
    Collection1.print(m.keySet());
  }
  // Producing a Collection of the values:
  public static void printValues(Map m) {
    System.out.print("Values: ");
    Collection1.print(m.values());
  }
  // Iterating through Map.Entry objects (pairs):
  public static void print(Map m) {
    Collection entries = m.entries();
    Iterator it = entries.iterator();
    while(it.hasNext()) {
      Map.Entry e = (Map.Entry)it.next();
      System.out.println("Key = " + e.getKey() +
        ", Value = " + e.getValue());
    }
  }
  public static void test(Map m) {
    fill(m, testData1);
    // Map has 'Set' behavior for keys:
    fill(m, testData1);
    printKeys(m);
    printValues(m);
    print(m);
    String key = testData1[4][0];
    String value = testData1[4][1];
    System.out.println("m.containsKey(\"" + key +
      "\"): " + m.containsKey(key));
    System.out.println("m.get(\"" + key + "\"): "
      + m.get(key));
    System.out.println("m.containsValue(\"" 
      + value + "\"): " + 
      m.containsValue(value)); 
    Map m2 = fill(new TreeMap(), testData2);
    m.putAll(m2);
    printKeys(m);
    m.remove(testData2[0][0]);
    printKeys(m);
    m.clear();
    System.out.println("m.isEmpty(): " 
      + m.isEmpty());
    fill(m, testData1);
    // Operations on the Set change the Map:
    m.keySet().removeAll(m.keySet());
    System.out.println("m.isEmpty(): " 
      + m.isEmpty());
  }
  public static void main(String args[]) {
    System.out.println("Testing HashMap");
    test(new HashMap());
    System.out.println("Testing TreeMap");
    test(new TreeMap());
  }
} ///:~

printKeys(),printValues()以及print()方法並不只是有用的工具,它們也清楚地揭示了一個Map的Collection“景象”的產生過程。keySet()方法會產生一個Set,它由Map中的鍵後推得來。在這兒,它只被當作一個Collection對待。values()也得到了類似的對待,它的作用是產生一個List,其中包含了Map中的所有值(注意鍵必須是獨一無二的,而值可以有重復)。由於這些Collection是由Map後推得到的,所以一個Collection中的任何改變都會在相應的Map中反映出來。
print()方法的作用是收集由entries產生的Iterator(反復器),並用它同時打印出每個“鍵-值”對的鍵和值。程序剩余的部分提供了每種Map操作的簡單示例,並對每種類型的Map進行了測試。
當創建自己的類,將其作為Map中的一個鍵使用時,必須注意到和以前的Set相同的問題。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved