Java基础学习札记之七(3)-Map接口

Java基础学习笔记之七(3)--Map接口

***Map<K,V>接口  

  An object that maps keys to values. A map cannot contain 
duplicate keys; each key can map to at most one value.  
  将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射一个值。   
  map中key不能重复,用hashCode进行比较,效果和用equals比较一样,
用hashCode比较会更快一些。

常用方法:

  1.Object put(Object key,Object value)  
  Associates the specified value with the specified key in this map
  将指定的值与此映射中的指定键相关联。
  关于返回值:如果map中这个key以前不存在,则直接放入,返回一个null,
如果map以前存在这个key,则新value替换旧value,同时返回value.

  2.Object get(Object key)
  Returns the value to which the specified key is mapped.
  返回此映射中映射到指定键的值
  
  3.Object remove(Object key);
  Removes the mapping for a key from this map if it is present
  如果存在此键的映射关系,则将其从映射中移除
  
  4.boolean containskey(Object key);
  Returns true if this map contains a mapping for the specified key
  如果此映射包含指定键的映射关系,则返回 true。
  
  5.boolean containsValue(Object value)
  Returns true if this map maps one or more keys to the specified value
  如果此映射为指定值映射一个或多个键,则返回 true。
  
  6.int size();
  Returns the number of key-value mappings in this map
  返回此映射中的键-值映射关系数

  7.boolean isEmpty()
  Returns true if this map contains no key-value mappings.
  如果此映射未包含键-值映射关系,则返回 true。  
  
  8.void putAll(Map map);
  Copies all of the mappings from the specified map to this map
  从指定映射中将所有映射关系复制到此映射中(可选操作)。
  
  9.void clear();
  Removes all of the mappings from this map.
  从此映射中移除所有映射关系