利用Maps
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());
}
} ///:~
#p#分页标题#e#
printKeys(),printValues()以及print()要领并不可是有用的东西,它们也清楚地展现了一个Map的Collection“情形”的发生进程。keySet()要了解发生一个Set,它由Map中的键后推得来。在这儿,它只被看成一个Collection看待。values()也获得了雷同的看待,它的浸染是发生一个List,个中包括了Map中的所有值(留意键必需是唯一无二的,而值可以有反复)。由于这些Collection是由Map后推获得的,所以一个Collection中的任何改变城市在相应的Map中反应出来。
print()要领的浸染是收集由entries发生的Iterator(重复器),并用它同时打印出每个“键-值”对的键和值。措施剩余的部门提供了每种Map操纵的简朴示例,并对每种范例的Map举办了测试。
当建设本身的类,将其作为Map中的一个键利用时,必需留意到和以前的Set沟通的问题。