
map(接口) 维持“键-值”对应关系(对),以便通过一个键查找相应的值
hashmap* 基于一个散列表实现(用它代替hashtable)。针对“键-值”对的插入和检索,这种形式具有最稳定的性能。可通过构建器对这一性能进行调整,以便设置散列表的“能力”和“装载因子”
arraymap 由一个arraylist后推得到的map。对反复的顺序提供了精确的控制。面向非常小的map设计,特别是那些需要经常创建和删除的。对于非常小的map,创建和反复所付出的代价要比hashmap低得多。但在map变大以后,性能也会相应地大幅度降低
treemap 在一个“红-黑”树的基础上实现。查看键或者“键-值”对时,它们会按固定的顺序排列(取决于comparable或comparator,稍后即会讲到)。treemap最大的好处就是我们得到的是已排好序的结果。treemap是含有submap()方法的唯一一种map,利用它可以返回树的一部分
下例包含了两套测试数据以及一个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相同的问题。
闽公网安备 35060202000074号