Java Map Interface源码注释翻译

接口定义说明

/**
 * An object that maps keys to values.  A map cannot contain duplicate keys;
 * each key can map to at most one value.
 * 一个K、V映射表对象,映射表不能包含重复key,每个key可以映射多个相同value。
 *
 * <p>This interface takes the place of the <tt>Dictionary</tt> class, which
 * was a totally abstract class rather than an interface.
 * 这个接口代替字典类,它是个完全抽象类而不是一个接口
 * 话外音:Map接口以接口形式出现,它规定了map的行为与返回。实现类类通过接口继承实现。
 *
 * <p>The <tt>Map</tt> interface provides three <i>collection views</i>, which
 * allow a map's contents to be viewed as a set of keys, collection of values,
 * or set of key-value mappings.  The <i>order</i> of a map is defined as
 * the order in which the iterators on the map's collection views return their
 * elements.  Some map implementations, like the <tt>TreeMap</tt> class, make
 * specific guarantees as to their order; others, like the <tt>HashMap</tt>
 * class, do not.
 * Map接口提供三个集合视图,包含keys set、value collection、key-value set。
 * 映射表的集合视图返回的元素次序,被定义为迭代器的次序。
 * 一些映射表实现者,像TressMap保证元素的次序;其它的,像HashMap则不是。
 *
 * <p>Note: great care must be exercised if mutable objects are used as map
 * keys.  The behavior of a map is not specified if the value of an object is
 * changed in a manner that affects <tt>equals</tt> comparisons while the
 * object is a key in the map.  A special case of this prohibition is that it
 * is not permissible for a map to contain itself as a key.  While it is
 * permissible for a map to contain itself as a value, extreme caution is
 * advised: the <tt>equals</tt> and <tt>hashCode</tt> methods are no longer
 * well defined on such a map.
 * 注意:必须非常关注运用中如果映射表的keys使用的易变对象,必须非常小心。
 * 一个映射表的行为没有详细说明,如果一个对象值以某种方式被改变了,
 * 在这个对象作为映射表的一个key期间,它影响了equals方法的比较。
 * 这个禁令的一个特殊场景,它不允许一个映射表包含自己作为一个key。
 * 尽管它允许自己作为一个value,非常谨慎的建议:equals、hashCode方法已经没有明确定义在这个映射表。
 *
 * <p>All general-purpose map implementation classes should provide two
 * "standard" constructors: a void (no arguments) constructor which creates an
 * empty map, and a constructor with a single argument of type <tt>Map</tt>,
 * which creates a new map with the same key-value mappings as its argument.
 * In effect, the latter constructor allows the user to copy any map,
 * producing an equivalent map of the desired class.  There is no way to
 * enforce this recommendation (as interfaces cannot contain constructors) but
 * all of the general-purpose map implementations in the JDK comply.
 * 所有通用的映射表实现类应该支持两个标准构造器:
 * 一个无参构造器创建一个空的映射表;
 * 另一个是包含类型为Map的一个单参,它创建一个包含相同key-value映射关系作为他们参数的新映射表。
 * 事实上,后者允许用户复制任何映射表,产生一个等价的所需映射表类。
 * 这里没法强制这条建议(作为接口不能包含构造器),但是所有在JDK中通用映射表实现遵守。
 *
 * <p>The "destructive" methods contained in this interface, that is, the
 * methods that modify the map on which they operate, are specified to throw
 * <tt>UnsupportedOperationException</tt> if this map does not support the
 * operation.  If this is the case, these methods may, but are not required
 * to, throw an <tt>UnsupportedOperationException</tt> if the invocation would
 * have no effect on the map.  For example, invoking the {@link #putAll(Map)}
 * method on an unmodifiable map may, but is not required to, throw the
 * exception if the map whose mappings are to be "superimposed" is empty.
 * 在接口里包含一些破坏性方法,即这些方法在它们操作中修改映射表。
 * 如果这个映射表不支持此类操作要特定抛出UnsupportedOperationException。
 * 在这种场景下,如果调用对映射表不影响,这些方法可以但不要求抛出UnsupportedOperationException。
 * 例如:在不修改映射表调用putAll方法可以,但不要求抛出异常。除非如果要覆盖的映射表是空的。
 *
 * <p>Some map implementations have restrictions on the keys and values they
 * may contain.  For example, some implementations prohibit null keys and
 * values, and some have restrictions on the types of their keys.  Attempting
 * to insert an ineligible key or value throws an unchecked exception,
 * typically <tt>NullPointerException</tt> or <tt>ClassCastException</tt>.
 * Attempting to query the presence of an ineligible key or value may throw an
 * exception, or it may simply return false; some implementations will exhibit
 * the former behavior and some will exhibit the latter.  More generally,
 * attempting an operation on an ineligible key or value whose completion
 * would not result in the insertion of an ineligible element into the map may
 * throw an exception or it may succeed, at the option of the implementation.
 * Such exceptions are marked as "optional" in the specification for this
 * interface.
 * 一些映射表实现可能制定约束条件在key与value上。
 * 例如:一些实现进制空的key和value,还有一些限制他们的key的类型。
 * 试图插入一个非法key或value会抛出未经过检查异常。
 * 典型代表:NullPointerException、ClassCastException。
 * 试图查询一个非法key或value的存在可能会抛出一个异常,或者会直接返回false;
 * 一些实现将要展现前者行为或后者行为。
 * 更普遍的,试图非法key或value的一个操作,其不会将非法元素放入映射表完成一个写入结果。
 * 可能会抛出一个异常或也可能会成功,具体操作在于实现。
 * 接口中规定了此类异常的标记为可选
 *
 * <p>Many methods in Collections Framework interfaces are defined
 * in terms of the {@link Object#equals(Object) equals} method.  For
 * example, the specification for the {@link #containsKey(Object)
 * containsKey(Object key)} method says: "returns <tt>true</tt> if and
 * only if this map contains a mapping for a key <tt>k</tt> such that
 * <tt>(key==null ? k==null : key.equals(k))</tt>." This specification should
 * <i>not</i> be construed to imply that invoking <tt>Map.containsKey</tt>
 * with a non-null argument <tt>key</tt> will cause <tt>key.equals(k)</tt> to
 * be invoked for any key <tt>k</tt>.  Implementations are free to
 * implement optimizations whereby the <tt>equals</tt> invocation is avoided,
 * for example, by first comparing the hash codes of the two keys.  (The
 * {@link Object#hashCode()} specification guarantees that two objects with
 * unequal hash codes cannot be equal.)  More generally, implementations of
 * the various Collections Framework interfaces are free to take advantage of
 * the specified behavior of underlying {@link Object} methods wherever the
 * implementor deems it appropriate.
 * 集合框架接口定义了很多方法,就equal方法而言。
 * 例如,containsKey方法规范中:"当且仅当映射表k中包含此key,才返回true,
 * 具体为(key==null ? k==null : key.equals(k))"。本规范不应该理解为隐射为
 * 使用非空参数key调用Map.containsKey,会导致key.equals(k)被任何key调用。
 * 实现可以自由实现优化,从而避免调用equals。
 * 例如,首先比较两个key的散列码。(Object#hashCode()规范保证了两个对象的散列码不相等对象不能相等)
 * 通常,在实现者认为合适的地方,各种集合框架接口的实现都可自由利用基于Object方法基础下的指定行为。
 *
 * <p>Some map operations which perform recursive traversal of the map may fail
 * with an exception for self-referential instances where the map directly or
 * indirectly contains itself. This includes the {@code clone()},
 * {@code equals()}, {@code hashCode()} and {@code toString()} methods.
 * Implementations may optionally handle the self-referential scenario, however
 * most current implementations do not do so.
 * 许多映射表在映射表中递归遍历,直接或间接包含自身可能会出现引用自身实例操作的异常失败。
 * 这里包含的方法:clone、equals、hashCode、toString。
 * 实现可以有选择处理自引用场景,然后大多数当前实现不这样做。
 *
 * <p>This interface is a member of the
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
 * Java Collections Framework</a>.
 *
 * @param <K> the type of keys maintained by this map
 *           映射表维护key的类型
 * @param <V> the type of mapped values
 *           映射值得类型
 *
 *
 * @author  Josh Bloch
 * @see HashMap
 * @see TreeMap
 * @see Hashtable
 * @see SortedMap
 * @see Collection
 * @see Set
 * @since 1.2
 */

查询操作 Query Operations

int size()

/**
 * Returns the number of key-value mappings in this map.  If the
 * map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
 * <tt>Integer.MAX_VALUE</tt>.
 * 返回映射表key-value映射数量。
 * 如果映射表包含元素数量超过Integer.MAX_VALUE,则返回Integer.MAX_VALUE
 *
 * @return the number of key-value mappings in this map
 */
int size();

boolean isEmpty();

/**
     * Returns <tt>true</tt> if this map contains no key-value mappings.
     * 如果没有包含key-value映射返回true。
     *
     * @return <tt>true</tt> if this map contains no key-value mappings
     */
    boolean isEmpty();

boolean containsKey();

/**
 * Returns <tt>true</tt> if this map contains a mapping for the specified
 * key.  More formally, returns <tt>true</tt> if and only if
 * this map contains a mapping for a key <tt>k</tt> such that
 * <tt>(key==null ? k==null : key.equals(k))</tt>.  (There can be
 * at most one such mapping.)
 * (最多可以有一个这样的映射)
 *
 * @param key key whose presence in this map is to be tested
 *            用于测试key是否存在这个映射表中
 * @return <tt>true</tt> if this map contains a mapping for the specified
 *         key
 *         如果映射表包含指定key的一个映射,返回true。
 * @throws ClassCastException if the key is of an inappropriate type for
 *         this map
 *         如果key是一个不当的类型,throws ClassCastException
 * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
 * @throws NullPointerException if the specified key is null and this map
 *         does not permit null keys
 *         如果指定key为null并且这个映射表不允许空key throws NullPointerException
 * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
 */

boolean containsKey(Object key);

boolean containsValue();

/**
 * Returns <tt>true</tt> if this map maps one or more keys to the
 * specified value.  More formally, returns <tt>true</tt> if and only if
 * this map contains at least one mapping to a value <tt>v</tt> such that
 * <tt>(value==null ? v==null : value.equals(v))</tt>.  This operation
 * will probably require time linear in the map size for most
 * implementations of the <tt>Map</tt> interface.
 * 如果映射表一个或多个key映射了指定的value,返回true。
 * 更准确讲,当且仅当此映射表至少包含一个与value的映射,
 * (value==null ? v==null : value.equals(v))。
 * 对于Map接口大多实现,此操作可能需要时间与映射表大小成线性关系。
 *
 * @param value value whose presence in this map is to be tested
 * @return <tt>true</tt> if this map maps one or more keys to the
 *         specified value
 * @throws ClassCastException if the value is of an inappropriate type for
 *         this map
 * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
 * @throws NullPointerException if the specified value is null and this
 *         map does not permit null values
 * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
 */
boolean containsValue(Object value);

V get(Object key);

/**
 * Returns the value to which the specified key is mapped,
 * or {@code null} if this map contains no mapping for the key.
 * 返回指定key映射的value,如果映射表包含没有映射此key则返回null。
 *
 * <p>More formally, if this map contains a mapping from a key
 * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
 * key.equals(k))}, then this method returns {@code v}; otherwise
 * it returns {@code null}.  (There can be at most one such mapping.)
 * 更严谨讲,对于key-value对,如果此映射表包含一个映射那就是
 * (key==null ? k==null : key.equals(k)),然后此方法返回v;
 * 否则返回null。(最多可以有一个这样的映射)
 *
 * <p>If this map permits null values, then a return value of
 * {@code null} does not <i>necessarily</i> indicate that the map
 * contains no mapping for the key; it's also possible that the map
 * explicitly maps the key to {@code null}.  The {@link #containsKey
 * containsKey} operation may be used to distinguish these two cases.
 * 如果此映射表允许null值,则返回值null不一定表示此映射表不包含该key的映射;
 * 它可能是将key映射了null。
 * containsKey 操作可用于区分这两种场景。
 * 不能讲get方法返回值作为key是否存在map的依据!!!
 *
 * @param key the key whose associated value is to be returned
 * @return the value to which the specified key is mapped, or
 *         {@code null} if this map contains no mapping for the key
 * @throws ClassCastException if the key is of an inappropriate type for
 *         this map
 * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
 * @throws NullPointerException if the specified key is null and this map
 *         does not permit null keys
 * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
 */
V get(Object key);

修改操作 Modification Operations

V put(K key, V value);

/**
 * Associates the specified value with the specified key in this map
 * (optional operation).  If the map previously contained a mapping for
 * the key, the old value is replaced by the specified value.  (A map
 * <tt>m</tt> is said to contain a mapping for a key <tt>k</tt> if and only
 * if {@link #containsKey(Object) m.containsKey(k)} would return
 * <tt>true</tt>.)
 * 如果key已存在集合则旧值被替换。当且仅当m.containsKey(k)返回true。
 *
 * @param key key with which the specified value is to be associated
 * @param value value to be associated with the specified key
 * @return the previous value associated with <tt>key</tt>, or
 *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
 *         (A <tt>null</tt> return can also indicate that the map
 *         previously associated <tt>null</tt> with <tt>key</tt>,
 *         if the implementation supports <tt>null</tt> values.)
 * @throws UnsupportedOperationException if the <tt>put</tt> operation
 *         is not supported by this map
 * @throws ClassCastException if the class of the specified key or value
 *         prevents it from being stored in this map
 * @throws NullPointerException if the specified key or value is null
 *         and this map does not permit null keys or values
 * @throws IllegalArgumentException if some property of the specified key
 *         or value prevents it from being stored in this map
 */
V put(K key, V value);

V remove(Object key);

/**
 * Removes the mapping for a key from this map if it is present
 * (optional operation).   More formally, if this map contains a mapping
 * from key <tt>k</tt> to value <tt>v</tt> such that
 * <code>(key==null ?  k==null : key.equals(k))</code>, that mapping
 * is removed.  (The map can contain at most one such mapping.)
 * 正常情况,满足(key==null ?  k==null : key.equals(k)),key的映射会被删除。
 *
 * <p>Returns the value to which this map previously associated the key,
 * or <tt>null</tt> if the map contained no mapping for the key.
 * 返回此映射先前与之关联的键的值,如果此映射表不包含这个键的映射则返回null。
 *
 * <p>If this map permits null values, then a return value of
 * <tt>null</tt> does not <i>necessarily</i> indicate that the map
 * contained no mapping for the key; it's also possible that the map
 * explicitly mapped the key to <tt>null</tt>.
 * 如果映射表允许null值,那么返回null值并不一定表示此映射表不包含此key的映射;
 * 它有可能是映射表已明确了此key的值为null。
 *
 * <p>The map will not contain a mapping for the specified key once the
 * call returns.
 * 一旦调用返回后,此映射表将不再包含这个key的映射关系。
 *
 * @param key key whose mapping is to be removed from the map
 * @return the previous value associated with <tt>key</tt>, or
 *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
 * @throws UnsupportedOperationException if the <tt>remove</tt> operation
 *         is not supported by this map
 * @throws ClassCastException if the key is of an inappropriate type for
 *         this map
 * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
 * @throws NullPointerException if the specified key is null and this
 *         map does not permit null keys
 * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
 */
V remove(Object key);

批量操作 Bulk Operations

void putAll(Map<? extends K, ? extends V> m);

/**
 * Copies all of the mappings from the specified map to this map
 * (optional operation).  The effect of this call is equivalent to that
 * of calling {@link #put(Object,Object) put(k, v)} on this map once
 * for each mapping from key <tt>k</tt> to value <tt>v</tt> in the
 * specified map.  The behavior of this operation is undefined if the
 * specified map is modified while the operation is in progress.
 * 将所有映射复制到此映射表中。
 * 效果等同于在此指定映射表遍历调用put(k, v)。
 * 如果在操作进行过程中修改了指定的映射,则此操作的行为是不确定的。
 *
 * @param m mappings to be stored in this map
 * @throws UnsupportedOperationException if the <tt>putAll</tt> operation
 *         is not supported by this map
 * @throws ClassCastException if the class of a key or value in the
 *         specified map prevents it from being stored in this map
 * @throws NullPointerException if the specified map is null, or if
 *         this map does not permit null keys or values, and the
 *         specified map contains null keys or values
 * @throws IllegalArgumentException if some property of a key or value in
 *         the specified map prevents it from being stored in this map
 */
void putAll(Map<? extends K, ? extends V> m);

void clear();

/**
 * Removes all of the mappings from this map (optional operation).
 * The map will be empty after this call returns.
 * 删除此映射表所有映射。
 * 调用返回后,此映射表将为空。
 *
 * @throws UnsupportedOperationException if the <tt>clear</tt> operation
 *         is not supported by this map
 */
void clear();

查看 Views

Set keySet();

/**
 * Returns a {@link Set} view of the keys contained in this map.
 * The set is backed by the map, so changes to the map are
 * reflected in the set, and vice-versa.  If the map is modified
 * while an iteration over the set is in progress (except through
 * the iterator's own <tt>remove</tt> operation), the results of
 * the iteration are undefined.  The set supports element removal,
 * which removes the corresponding mapping from the map, via the
 * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
 * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
 * operations.  It does not support the <tt>add</tt> or <tt>addAll</tt>
 * operations.
 * 返回此映射表包含键的视图。
 * set依赖map,因此对map的更改会反映在set中,反之亦然。
 * 如果在对set进行迭代时修改了map(排除通过迭代器的remove操作),
 * 此结果在迭代中是不确定的。
 * set是支持元素移除的,它删除了映射表的对应映射。
 * 可借助于Iterator.remove、Set.remove、removeAll、retainAll、clear。
 * 它不支持add与addAll操作。
 *
 * @return a set view of the keys contained in this map
 */
Set<K> keySet();

Collection values();

/**
 * Returns a {@link Collection} view of the values contained in this map.
 * The collection is backed by the map, so changes to the map are
 * reflected in the collection, and vice-versa.  If the map is
 * modified while an iteration over the collection is in progress
 * (except through the iterator's own <tt>remove</tt> operation),
 * the results of the iteration are undefined.  The collection
 * supports element removal, which removes the corresponding
 * mapping from the map, via the <tt>Iterator.remove</tt>,
 * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
 * <tt>retainAll</tt> and <tt>clear</tt> operations.  It does not
 * support the <tt>add</tt> or <tt>addAll</tt> operations.
 * 返回此映射表包含值得视图。
 * collection依赖map,因此对map的更改会反映在collection中,反之亦然。
 * 如果在对collection进行迭代时修改了map(排除通过迭代器的remove操作),
 * 此结果在迭代中是不确定的。
 * collection是支持元素移除的,它删除了映射表的对应映射。
 * 可借助于Iterator.remove、Collection.remove、removeAll、retainAll、clear。
 *
 * @return a collection view of the values contained in this map
 */
Collection<V> values();

Set<Map.Entry<K, V>> entrySet();

/**
 * Returns a {@link Set} view of the mappings contained in this map.
 * The set is backed by the map, so changes to the map are
 * reflected in the set, and vice-versa.  If the map is modified
 * while an iteration over the set is in progress (except through
 * the iterator's own <tt>remove</tt> operation, or through the
 * <tt>setValue</tt> operation on a map entry returned by the
 * iterator) the results of the iteration are undefined.  The set
 * supports element removal, which removes the corresponding
 * mapping from the map, via the <tt>Iterator.remove</tt>,
 * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
 * <tt>clear</tt> operations.  It does not support the
 * <tt>add</tt> or <tt>addAll</tt> operations.
 *
 * @return a set view of the mappings contained in this map
 */
Set<Map.Entry<K, V>> entrySet();

比较与哈希 Comparison and hashing

boolean equals(Object o);

/**
 * Compares the specified object with this map for equality.  Returns
 * <tt>true</tt> if the given object is also a map and the two maps
 * represent the same mappings.  More formally, two maps <tt>m1</tt> and
 * <tt>m2</tt> represent the same mappings if
 * <tt>m1.entrySet().equals(m2.entrySet())</tt>.  This ensures that the
 * <tt>equals</tt> method works properly across different implementations
 * of the <tt>Map</tt> interface.
 * 比较指定映射表与此映射表是否相等。
 * 如果给出的对象同样是映射表,并且两个映射表存在相同映射则返回true。
 * 相等满足m1.entrySet().equals(m2.entrySet())
 * 这里要保证equals方法可以在不同映射表接口实现工作。
 *
 * @param o object to be compared for equality with this map
 * @return <tt>true</tt> if the specified object is equal to this map
 */
boolean equals(Object o);

int hashCode();

/**
 * Returns the hash code value for this map.  The hash code of a map is
 * defined to be the sum of the hash codes of each entry in the map's
 * <tt>entrySet()</tt> view.  This ensures that <tt>m1.equals(m2)</tt>
 * implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps
 * <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of
 * {@link Object#hashCode}.
 * 返回此映射表的散列码。
 * 映射表的散列码被定义为映射表每个实体散列码之和。
 * 这里要保证两个映射表m1.equals(m2)同时也是m1.hashCode()==m2.hashCode()
 *
 * @return the hash code value for this map
 * @see Map.Entry#hashCode()
 * @see Object#equals(Object)
 * @see #equals(Object)
 */
int hashCode();

interface Entry<K,V>

/**
 * A map entry (key-value pair).  The <tt>Map.entrySet</tt> method returns
 * a collection-view of the map, whose elements are of this class.  The
 * <i>only</i> way to obtain a reference to a map entry is from the
 * iterator of this collection-view.  These <tt>Map.Entry</tt> objects are
 * valid <i>only</i> for the duration of the iteration; more formally,
 * the behavior of a map entry is undefined if the backing map has been
 * modified after the entry was returned by the iterator, except through
 * the <tt>setValue</tt> operation on the map entry.
 * 一个键值对映射表条目。
 * Map.entrySet方法返回此映射表一个集合视图,它的元素就是此类。
 * 从迭代器中的集合视图中获得映射表引用条目,只有这么一种方式。
 * Map.Entry这些对象仅在迭代期间有效。
 * 如果依赖中的映射表在被迭代器返回后被修改,映射表的行为不确定,
 * 除非通过使用映射表entry的setValue操作。
 *
 * @see Map#entrySet()
 * @since 1.2
 */

K getKey();

/**
 * Returns the key corresponding to this entry.
 * 返回此条目对应的key
 *
 * @return the key corresponding to this entry
 * @throws IllegalStateException implementations may, but are not
 *         required to, throw this exception if the entry has been
 *         removed from the backing map.
 */
K getKey();

V getValue();

/**
 * Returns the value corresponding to this entry.  If the mapping
 * has been removed from the backing map (by the iterator's
 * <tt>remove</tt> operation), the results of this call are undefined.
 * 返回此条目对应的值
 * 如果从依赖的映射表删除此映射(被迭代器的remove操作),其调用结果不确定。
 *
 * @return the value corresponding to this entry
 * @throws IllegalStateException implementations may, but are not
 *         required to, throw this exception if the entry has been
 *         removed from the backing map.
 */
V getValue();

V setValue(V value);

/**
 * Replaces the value corresponding to this entry with the specified
 * value (optional operation).  (Writes through to the map.)  The
 * behavior of this call is undefined if the mapping has already been
 * removed from the map (by the iterator's <tt>remove</tt> operation).
 * 用指定值替换此条目中对应的值。
 * (写入映射表中) 如果已存在的映射被从映射表移除,此调用行为不确定
 *
 * @param value new value to be stored in this entry
 * @return old value corresponding to the entry
 * @throws UnsupportedOperationException if the <tt>put</tt> operation
 *         is not supported by the backing map
 * @throws ClassCastException if the class of the specified value
 *         prevents it from being stored in the backing map
 * @throws NullPointerException if the backing map does not permit
 *         null values, and the specified value is null
 * @throws IllegalArgumentException if some property of this value
 *         prevents it from being stored in the backing map
 * @throws IllegalStateException implementations may, but are not
 *         required to, throw this exception if the entry has been
 *         removed from the backing map.
 */
V setValue(V value);

boolean equals(Object o);

/**
 * Compares the specified object with this entry for equality.
 * Returns <tt>true</tt> if the given object is also a map entry and
 * the two entries represent the same mapping.  More formally, two
 * entries <tt>e1</tt> and <tt>e2</tt> represent the same mapping
 * if<pre>
 *     (e1.getKey()==null ?
 *      e2.getKey()==null : e1.getKey().equals(e2.getKey()))  &amp;&amp;
 *     (e1.getValue()==null ?
 *      e2.getValue()==null : e1.getValue().equals(e2.getValue()))
 * </pre>
 * This ensures that the <tt>equals</tt> method works properly across
 * different implementations of the <tt>Map.Entry</tt> interface.
 * 指定对象与此条目比较相等。
 * 如果给出的对象同样是一个映射表条目,并且两个条目存在一样映射,返回true。
 * 这里要保证equals方法在不同May.Entry实现可以work。
 *
 * @param o object to be compared for equality with this map entry
 * @return <tt>true</tt> if the specified object is equal to this map
 *         entry
 */
boolean equals(Object o);

int hashCode();

/**
 * Returns the hash code value for this map entry.  The hash code
 * of a map entry <tt>e</tt> is defined to be: <pre>
 *     (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
 *     (e.getValue()==null ? 0 : e.getValue().hashCode())
 * </pre>
 * This ensures that <tt>e1.equals(e2)</tt> implies that
 * <tt>e1.hashCode()==e2.hashCode()</tt> for any two Entries
 * <tt>e1</tt> and <tt>e2</tt>, as required by the general
 * contract of <tt>Object.hashCode</tt>.
 * 返回此映射表条目的散列码。
 * 这里要保证e1.equals(e2)意味着e1.hashCode()==e2.hashCode()
 *
 * @return the hash code value for this map entry
 * @see Object#hashCode()
 * @see Object#equals(Object)
 * @see #equals(Object)
 */
int hashCode();

默认方法 Defaultable methods @since 1.8

default V getOrDefault(Object key, V defaultValue)

default void forEach(BiConsumer<? super K, ? super V> action)

default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function)

default V putIfAbsent(K key, V value)

default boolean remove(Object key, Object value)

default boolean replace(K key, V oldValue, V newValue)

default V replace(K key, V value)

default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)

default V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)

default V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)

default V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是符合要求的Java代码: ```java import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); StringBuilder sb = new StringBuilder(); String line; while (!(line = scanner.nextLine()).equals("exit")) { sb.append(line).append('\n'); } String sourceCode = sb.toString(); if (sourceCode.trim().isEmpty()) { System.out.println("Wrong Format"); return; } Set<String> keywords = new HashSet<>(Arrays.asList( "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "final", "finally", "float", "for", "if", "goto", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while" )); Map<String, Integer> keywordCount = new TreeMap<>(); int i = 0; while (i < sourceCode.length()) { char c = sourceCode.charAt(i); if (c == '/') { if (i + 1 < sourceCode.length() && sourceCode.charAt(i + 1) == '/') { i = sourceCode.indexOf('\n', i + 2); if (i == -1) { break; } } else if (i + 1 < sourceCode.length() && sourceCode.charAt(i + 1) == '*') { i = sourceCode.indexOf("*/", i + 2); if (i == -1) { break; } i += 2; } else { i++; } } else if (c == '\"') { i++; while (i < sourceCode.length() && sourceCode.charAt(i) != '\"') { if (sourceCode.charAt(i) == '\\') { i++; } i++; } i++; } else if (c == '\'') { i++; while (i < sourceCode.length() && sourceCode.charAt(i) != '\'') { if (sourceCode.charAt(i) == '\\') { i++; } i++; } i++; } else if (Character.isLetter(c) || c == '_') { int j = i + 1; while (j < sourceCode.length() && (Character.isLetterOrDigit(sourceCode.charAt(j)) || sourceCode.charAt(j) == '_')) { j++; } String word = sourceCode.substring(i, j); if (keywords.contains(word)) { keywordCount.put(word, keywordCount.getOrDefault(word, 0) + 1); } i = j; } else { i++; } } if (keywordCount.isEmpty()) { System.out.println(); } else { for (Map.Entry<String, Integer> entry : keywordCount.entrySet()) { System.out.printf("%d\t%s\n", entry.getValue(), entry.getKey()); } } } } ``` 程序的思路如下: 1. 读入输入的Java源码,保存在一个字符串中。 2. 定义一个关键字集合和一个关键字计数的映射。 3. 遍历源码字符串,忽略注释和字符串中出现的关键字,对于其他出现的单词,如果是关键字则将其计数加一。 4. 输出统计结果,按照关键字升序排序。 该程序使用了 set 和 map 方法,并且代码在 60 行以内。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值