java映射接口 Map Interface之所以翻译这个接口,是为了便于下边一课--算法的翻译。A Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value. It models the mathematical function abstraction. The Map interface follows. 一个映射是一个对象,映射它所有的键到每个对应的值。一个映射不能包含重复的键:每个键只能映射最多到一个值。它的模型,是数学函数的抽象化。这个映射接口如下。
public interface Map<K,V> { // Basic operations // 基本操作 V put(K key, V value); V get(Object key); V remove(Object key); boolean containsKey(Object key); boolean containsValue(Object value); int size(); boolean isEmpty(); // Bulk operations // 大块操作 void putAll(Map<? extends K, ? extends V> m); void clear(); // Collection Views // 集合的视图 public Set<K> keySet(); public Collection<V> values(); public Set<Map.Entry<K,V>> entrySet(); // Interface for entrySet elements // 输入一个集合元素的接口 public interface Entry { K getKey(); V getValue(); V setValue(V value); } }
Comparison to Hashtable If you've used Hashtable, you're already familiar with the general basics of Map. (Of course, Map is an interface, while Hashtable is a concrete implementation.) The following are the major differences: Map provides Collection views instead of direct support for iteration via Enumeration objects. Collection views greatly enhance the expressiveness of the interface, as discussed later in this section. Map allows you to iterate over keys, values, or key-value pairs; Hashtable does not provide the third option. Map provides a safe way to remove entries in the midst of iteration; Hashtable did not. Finally, Map fixes a minor deficiency in the Hashtable interface. Hashtable has a method called contains, which returns true if the Hashtable contains a given value. Given its name, you'd expect this method to return true if the Hashtable contained a given key, because the key is the primary access mechanism for a Hashtable. The Map interface eliminates this source of confusion by renaming the method containsValue. Also, this improves the interface's consistency — containsValue parallels containsKey. Map Interface Basic Operations
Map Interface Basic OperationsThe basic operations of Map (put, get, containsKey, containsValue, size, and isEmpty) behave exactly like their counterparts in Hashtable. The following program generates a frequency table of the words found in its argument list. The frequency table maps each word to the number of times it occurs in the argument list. 映射接口的基本操作这个映射基础的操作[put放, get取, containsKey包含键, containsValue包含值, size大小, and isEmpty是否为空]如他们在hashtable中的行为一样。接下来的程序生成一个在其变量列表中,被找到的单词的频率表。这个频率表,映射了每个单词到它发生在变量列表中,发生的次数。 import java.util.*;public class Freq { public static void main(String[] args) { Map<String, Integer> m = new HashMap<String, Integer>(); // Initialize frequency table from command line // 从命令行初始化频率表 for (String a : args) { Integer freq = m.get(a); m.put(a, (freq == null) ? 1 : freq + 1); } System.out.println(m.size() + " distinct words:"); System.out.println(m); } }
这个程序唯一有趣的地方,是'放'段落第二个变量。这个变量是一个条件表达式,这个表达式在一个单词从来还没有被看见的情况下,或者这个单词已经被看见,但是大于他现在的所对应值的时候,设置频率为1。尝试运行程序,用下面的字符串。java Freq if it is to be it is up to me to delegate The program yields the following output. 这个程序将会产生如下的输出。8 distinct words: 8个明显的,清楚的单词。{to=3, delegate=1, be=1, it=2, up=1, if=1, me=1, is=2} Similarly, you could make the program print the frequency table in the order the words first appear on the command line simply by changing the implementation type of the map to LinkedHashMap. Doing so results in the following output. 同样的,你可以使这个程序打印出频率表,让单词首先显示在命令行,通过简单的改变小程序中,映射到被连接的哈希映射的映射类型。做这个给出的结果如下: 8 distinct words:8个明显的,清楚的单词。{if=1, it=2, is=2, to=3, be=1, up=1, me=1, delegate=1}This flexibility provides a potent illustration of the power of an interface-based framework. 这个灵活性提供了一个潜在的例子,充分显示以接口为基础的框架的威力。Like the Setand Listinterfaces, Map strengthens the requirements on the equals and hashCode methods so that two Map objects can be compared for logical equality without regard to their implementation types. Two Map instances are equal if they represent the same key-value mappings. 正如设置set和列出list接口,映射强化了equals和hashcode方法的需求,以至于,两个映射对象可以对比他们的逻辑相同性,而不需要考虑他们实例的类型。 两个映射的实例,或者说例子,程序,是相同的,如果他们表达同一个的键-值映射。 By convention, all general-purpose Map implementations provide constructors that take a Map object and initialize the new Map to contain all the key-value mappings in the specified Map. This standard Map conversion constructor is entirely analogous to the standard Collection constructor: It allows the caller to create a Map of a desired implementation type that initially contains all of the mappings in another Map, regardless of the other Map's implementation type. For example, suppose you have a Map, named m. The following one-liner creates a new HashMap initially containing all of the same key-value mappings as m. 但是相反的,所有一般的映射实例,提供了构造方法,这个方法可以带上一个映射对象,然后初始一个新的映射,这个新映射,包含所有键值映射在特定的映射中。 这个标准映射转化系统的构造方法,是全部相同于标准Collection constructor[非常重要的术语]的。它允许调用者来创建一个被需要的实例类型的映射,这个映射最初包含所有映射关系在另一个映射中,不需要考虑另外的映射的实例类型。 例如,假设你有个映射,叫做m。 接下来单线性-[直接]创造一个新的哈希映射,这个映射最初包含的,就是跟m所有键值关系相同的映射关系。Map<K, V> copy = new HashMap<K, V>(m);
The only tricky thing about this program is the second argument of the put statement. That argument is a conditional expression that has the effect of setting the frequency to one if the word has never been seen before or one more than its current value if the word has already been seen. Try running this program with the command: --修正上一段 提供对应英文 其中 '放'段落.....put statement应该是put的陈述表达式 这个程序唯一有趣的地方,是'放'段落第二个变量。这个变量是一个条件表达式,这个表达式在一个单词从来还没有被看见的情况下,或者 这个单词已经被看见,但是大于他现在的所对应值的时候,设置频率为1。
不需要译,你看到 Map, Hashtable 都是大写的么?那就直接使用原文。比如:A Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value. Map 是映射键-值的对象。一个映射不能含有重复的键:每个键最多只能映射一个值。
这里的 Map, Hashtable 指的是 JDK 类库中的接口/类,所以不需要译成中文的。
Map Interface Bulk Operations The clear operation does exactly what you would think it could do: It removes all the mappings from the Map. The putAll operation is the Map analogue of the Collection interface's addAll operation. In addition to its obvious use of dumping one Map into another, it has a second, more subtle use. Suppose a Map is used to represent a collection of attribute-value pairs; the putAll operation, in combination with the Map conversion constructor, provides a neat way to implement attribute map creation with default values. The following is a static factory method that demonstrates this technique. static <K, V> Map<K, V> newAttributeMap( Map<K, V>defaults, Map<K, V> overrides) { Map<K, V> result = new HashMap<K, V>(defaults); result.putAll(overrides); return result; }//我对程序的理解//我周围的人,把程序当作他们的母语来使用//我也受到这种感染,很多情况下,我都在使用英语---用的随心所欲,完全不需要语法。//Map就是一个动作,把关系画出来。 HashTable我看来100000%是一个特定的表Map接口大块操作 Clear 做的就是你可能想到他会做的,从Map中删除了所有的映射。putAll 操作是Map中,类似于Collection interface's 中 addAll的一个操作。另外,它的作用很明显,把一堆关系,从一个Map中一个个抽取,存放到另外一个 Map中。但是它有另外一种,更狡猾的作用。假设一个Map用来表示一组[属性--值]对。putAll联合了Map conversion constructor 提供了一个整洁的方法,实现不同值的属性map的创建。//个人说明,这个方法putAll,可以让你创建一个新的Map,这个Map保存的映射,恰恰就是原来的Map中的[属性---值]对 下面的静态工厂方法揭示了这个技巧[static factory method]static <K, V> Map<K, V> newAttributeMap( Map<K, V>defaults, Map<K, V> overrides) { Map<K, V> result = new HashMap<K, V>(defaults); result.putAll(overrides); return result; }//这个方法里面有三个map //defaults;overrides;results //利用overrides中转,results这个map[映射图]保存了defaults中不同值的attributes //各位可以尝试
// 基本操作
V put(K key, V value);
V get(Object key);
V remove(Object key);
boolean containsKey(Object key);
boolean containsValue(Object value);
int size();
boolean isEmpty(); // Bulk operations
// 大块操作
void putAll(Map<? extends K, ? extends V> m);
void clear(); // Collection Views
// 集合的视图
public Set<K> keySet();
public Collection<V> values();
public Set<Map.Entry<K,V>> entrySet(); // Interface for entrySet elements
// 输入一个集合元素的接口 public interface Entry {
K getKey();
V getValue();
V setValue(V value);
}
}
If you've used Hashtable, you're already familiar with the general basics of Map. (Of course, Map is an interface, while Hashtable is a concrete implementation.) The following are the major differences:
Map provides Collection views instead of direct support for iteration via Enumeration objects. Collection views greatly enhance the expressiveness of the interface, as discussed later in this section.
Map allows you to iterate over keys, values, or key-value pairs; Hashtable does not provide the third option.
Map provides a safe way to remove entries in the midst of iteration; Hashtable did not.
Finally, Map fixes a minor deficiency in the Hashtable interface. Hashtable has a method called contains, which returns true if the Hashtable contains a given value. Given its name, you'd expect this method to return true if the Hashtable contained a given key, because the key is the primary access mechanism for a Hashtable. The Map interface eliminates this source of confusion by renaming the method containsValue. Also, this improves the interface's consistency — containsValue parallels containsKey.
Map Interface Basic Operations
The following program generates a frequency table of the words found in its argument list.
The frequency table maps each word to the number of times it occurs in the argument list. 映射接口的基本操作这个映射基础的操作[put放, get取, containsKey包含键, containsValue包含值, size大小, and isEmpty是否为空]如他们在hashtable中的行为一样。接下来的程序生成一个在其变量列表中,被找到的单词的频率表。这个频率表,映射了每个单词到它发生在变量列表中,发生的次数。
import java.util.*;public class Freq {
public static void main(String[] args) {
Map<String, Integer> m = new HashMap<String, Integer>(); // Initialize frequency table from command line
// 从命令行初始化频率表
for (String a : args) {
Integer freq = m.get(a);
m.put(a, (freq == null) ? 1 : freq + 1);
} System.out.println(m.size() + " distinct words:");
System.out.println(m);
}
}
The program yields the following output. 这个程序将会产生如下的输出。8 distinct words: 8个明显的,清楚的单词。{to=3, delegate=1, be=1, it=2, up=1, if=1, me=1, is=2}
Similarly, you could make the program print the frequency table in the order the words first appear on the command line simply by changing the implementation type of the map to LinkedHashMap. Doing so results in the following output. 同样的,你可以使这个程序打印出频率表,让单词首先显示在命令行,通过简单的改变小程序中,映射到被连接的哈希映射的映射类型。做这个给出的结果如下:
8 distinct words:8个明显的,清楚的单词。{if=1, it=2, is=2, to=3, be=1, up=1, me=1, delegate=1}This flexibility provides a potent illustration of the power of an interface-based framework. 这个灵活性提供了一个潜在的例子,充分显示以接口为基础的框架的威力。Like the Setand Listinterfaces, Map strengthens the requirements on the equals and hashCode methods so that two Map objects can be compared for logical equality without regard to their implementation types. Two Map instances are equal if they represent the same key-value mappings.
正如设置set和列出list接口,映射强化了equals和hashcode方法的需求,以至于,两个映射对象可以对比他们的逻辑相同性,而不需要考虑他们实例的类型。 两个映射的实例,或者说例子,程序,是相同的,如果他们表达同一个的键-值映射。
By convention, all general-purpose Map implementations provide constructors that take a Map object and initialize the new Map to contain all the key-value mappings in the specified Map. This standard Map conversion constructor is entirely analogous to the standard Collection constructor: It allows the caller to create a Map of a desired implementation type that initially contains all of the mappings in another Map, regardless of the other Map's implementation type. For example, suppose you have a Map, named m. The following one-liner creates a new HashMap initially containing all of the same key-value mappings as m. 但是相反的,所有一般的映射实例,提供了构造方法,这个方法可以带上一个映射对象,然后初始一个新的映射,这个新映射,包含所有键值映射在特定的映射中。 这个标准映射转化系统的构造方法,是全部相同于标准Collection constructor[非常重要的术语]的。它允许调用者来创建一个被需要的实例类型的映射,这个映射最初包含所有映射关系在另一个映射中,不需要考虑另外的映射的实例类型。 例如,假设你有个映射,叫做m。 接下来单线性-[直接]创造一个新的哈希映射,这个映射最初包含的,就是跟m所有键值关系相同的映射关系。Map<K, V> copy = new HashMap<K, V>(m);
这个程序唯一有趣的地方,是'放'段落第二个变量。这个变量是一个条件表达式,这个表达式在一个单词从来还没有被看见的情况下,或者 这个单词已经被看见,但是大于他现在的所对应值的时候,设置频率为1。
你译成了:比较哈希表应译为:与 Hashtable 比较
Map, Hashtable 属于专有名词,在 Java 中是接口/类的名字,不能将其译成中文。
http://www.china-pub.com/35690
1,希望楼主能坚持把它译完;
2,支持原创、原译;
3,让更多的人了解 Java Tutorial
请高手指点。
Map 是映射键-值的对象。一个映射不能含有重复的键:每个键最多只能映射一个值。
The clear operation does exactly what you would think it could do: It removes all the mappings from the Map. The putAll operation is the Map analogue of the Collection interface's addAll operation. In addition to its obvious use of dumping one Map into another, it has a second, more subtle use. Suppose a Map is used to represent a collection of attribute-value pairs; the putAll operation, in combination with the Map conversion constructor, provides a neat way to implement attribute map creation with default values. The following is a static factory method that demonstrates this technique.
static <K, V> Map<K, V> newAttributeMap(
Map<K, V>defaults, Map<K, V> overrides) {
Map<K, V> result = new HashMap<K, V>(defaults);
result.putAll(overrides);
return result;
}//我对程序的理解//我周围的人,把程序当作他们的母语来使用//我也受到这种感染,很多情况下,我都在使用英语---用的随心所欲,完全不需要语法。//Map就是一个动作,把关系画出来。 HashTable我看来100000%是一个特定的表Map接口大块操作
Clear 做的就是你可能想到他会做的,从Map中删除了所有的映射。putAll 操作是Map中,类似于Collection interface's 中 addAll的一个操作。另外,它的作用很明显,把一堆关系,从一个Map中一个个抽取,存放到另外一个 Map中。但是它有另外一种,更狡猾的作用。假设一个Map用来表示一组[属性--值]对。putAll联合了Map conversion constructor 提供了一个整洁的方法,实现不同值的属性map的创建。//个人说明,这个方法putAll,可以让你创建一个新的Map,这个Map保存的映射,恰恰就是原来的Map中的[属性---值]对
下面的静态工厂方法揭示了这个技巧[static factory method]static <K, V> Map<K, V> newAttributeMap(
Map<K, V>defaults, Map<K, V> overrides) {
Map<K, V> result = new HashMap<K, V>(defaults);
result.putAll(overrides);
return result;
}//这个方法里面有三个map
//defaults;overrides;results
//利用overrides中转,results这个map[映射图]保存了defaults中不同值的attributes
//各位可以尝试
我认为,Map应该翻译成映射图,因为它保存了许多映射关系。Hashtable就是哈希表。我喜欢翻译
Map <K, V>defaults, Map <K, V> overrides) {
Map <K, V> result = new HashMap <K, V>(defaults);
result.putAll(overrides);
return result;
} 小代码,实现一个映射图保存另外一个映射图的属性-值对关系。
呵呵,哈希表也应该译成散列表,哈希表是旧译名了,已废弃不用了,而且哈希表的单词应该是 hashtable,而不是 Hashtable
在这里鄙视下某些java书翻译的内容,看得我想死的心都有了...