/**
* An object that maps keys to values. A map cannot contain duplicate keys;
* each key can map to at most one value.
*
* <p>This interface takes the place of the <tt>Dictionary</tt> class, which
* was a totally abstract class rather than an interface.
*
* <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.
*
* <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.
*
* <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.
*
* <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.
*
* <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.
*
* <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.
*
* <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.
*
* <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
* @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
*/
public interface Map<K,V>

Map是一个接口,一个map不能包含重复的key,每个key只能映射唯一一个value。

Map接口是用来取代Dictionary抽象类的。

Map接口提供三个集合视图,1.key的集合 2.value的集合 3.key-value的集合。map内元素的顺序取决于Iterator的具体实现,获取集合视图其实是获取一个迭代器,实现对遍历元素细节的隐藏。TreeMap类能保证遍历元素的顺序,而HashMap就无法保证遍历元素的顺序。

注意:当使用一个可变对象作为key的时候要小心,map是根据hashCode和equals方法决定存放的位置的。一个特殊的案例是不允许一个map将自己作为一个key,但允许将自己作为一value。

所有多种用途的map实现类应该提供两个“标准”构造器,一个无参构造器用来创建一个空map,一个只有一个参数,参数类型是map的构造器,用来创建一个新的和传入参数有一样key-value映射的map。实际上,后者允许复制任何一个map,这仅仅是一个建议,并没有强制要求,因为接口是无法包含构造器的,不过这个建议在JDK被遵守。

如果一个方法的操作是不被支持的,这个方法指定抛出UnsupportedOperationException异常。如果这个操作对mao是没有影响的,那么也可以不抛出UnsupportedOperationException异常。例如,在一个不能被修改的map调用putAll(Map)方法,如果该map的映射是空的,就不要求抛出UnsupportedOperationException异常。

Map接口是Java Collections Framework的一员。

Map里面的方法:

int size();//返回map中key-value映射的数量

boolean isEmpty();//如果map中没有key-value映射返回true

//如果map不含key映射,返回false,当key的类型不符合,抛出ClassCastException,当key是
//null且该map不支持key的值是null时,抛出NullPointerException
boolean containsKey(Object key);

//如果map含有一个以上的key映射的参数value,返回true,异常抛出的情况和containKey一样
boolean containsValue(Object value);

//根据key得到对应的value,如果没有对应的映射,返回null,如果map允许value为null,返回
//null可能是有一对key-null的映射或没有对应的映射
V get(Object key);

//往map放入一对key-value映射
V put(K key, V value);

//根据key删除对应映射
V remove(Object key);

//复制一份与参数一样的map
void putAll(Map<? extends K, ? extends V> m);

//清空map中所有的映射
void clear();

//返回map中所有key的集合
Set<K> keySet();

//返回map中所有value的集合
Collection<V> values();

//返回key-value的集合
Set<Map.Entry<K, V>> entrySet();

//比较调用者与参数是否相等
boolean equals(Object o);

//计算map的hash code
int hashCode(); //还有其他default方法...,都是jdk1.8发布的

Map接口里有一个内部接口Entry<K,V>,其实它就是Map存放key-value映射的数据结构

interface Entry<K,V> {

  //返回对应的key
K getKey();

  //返回对应的value
V getValue();

  //设置用新value替换旧value,返回值是旧value
V setValue(V value);

  //如果两个entry的映射一样,返回true
boolean equals(Object o);

  //计算entry的hash code
int hashCode();

  //下面的静态方法是JDK1.8才发布的
  //返回一个比较器,比较的规则是key的自然大小
public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> comparingByKey() {
return (Comparator<Map.Entry<K, V>> & Serializable)
(c1, c2) -> c1.getKey().compareTo(c2.getKey());//这里用的是lambda表达式
}
  
  
  //返回一个比较器,比较规则是value的自然大小
public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue() {
return (Comparator<Map.Entry<K, V>> & Serializable)
(c1, c2) -> c1.getValue().compareTo(c2.getValue());
}

  //返回一个比较器,比较规则用参数传入,比较的是key
public static <K, V> Comparator<Map.Entry<K, V>> comparingByKey(Comparator<? super K> cmp) {
Objects.requireNonNull(cmp);
return (Comparator<Map.Entry<K, V>> & Serializable)
(c1, c2) -> cmp.compare(c1.getKey(), c2.getKey());
}

  //返回一个比较器,比较规则用参数传入,比较的是value
public static <K, V> Comparator<Map.Entry<K, V>> comparingByValue(Comparator<? super V> cmp) {
Objects.requireNonNull(cmp);
return (Comparator<Map.Entry<K, V>> & Serializable)
(c1, c2) -> cmp.compare(c1.getValue(), c2.getValue());
}
}

java.util.Map源码分析的更多相关文章

  1. java.util.HashMap源码分析

    在java jdk8中对HashMap的源码进行了优化,在jdk7中,HashMap处理“碰撞”的时候,都是采用链表来存储,当碰撞的结点很多时,查询时间是O(n). 在jdk8中,HashMap处理“ ...

  2. java.util.Collection源码分析和深度讲解

    写在开头 java.util.Collection 作为Java开发最常用的接口之一,我们经常使用,今天我带大家一起研究一下Collection接口,希望对大家以后的编程以及系统设计能有所帮助,本文所 ...

  3. java.util.Hashtable源码分析

    Hashtable实现一个键值映射的表.任何非null的object可以用作key和value. 为了能存取对象,放在表里的对象必须实现hashCode和equals方法. 一个Hashtable有两 ...

  4. java.util.AbstractStringBuilder源码分析

    AbstractStringBuilder是一个抽象类,是StringBuilder和StringBuffer的父类,分析它的源码对StringBuilder和StringBuffer代码的理解有很大 ...

  5. java.util.Dictionary源码分析

    Dictionary是一个抽象类,Hashtable是它的一个子类. 类的声明:/** The <code>Dictionary</code> class is the abs ...

  6. java.util.TreeSet源码分析

    TreeSet是基于TreeMap实现的,元素的顺序取决于元素自身的自然顺序或者在构造时提供的比较器. 对于add,remove,contains操作,保证log(n)的时间复杂度. 因为Set接口的 ...

  7. java.util.TreeMap源码分析

    TreeMap的实现基于红黑树,排列的顺序根据key的大小,或者在创建时提供的比较器,取决于使用哪个构造器. 对于,containsKey,get,put,remove操作,保证时间复杂度为log(n ...

  8. java.util.HashSet源码分析

    public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, java. ...

  9. java.util.LinkedList源码分析

    public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, D ...

随机推荐

  1. Kooboo中怎么新增一个关联的Details 动态页面。

    Kooboo中怎么新增一个关联的Details 动态页面. 有几个要点: 1. Sub Page的Parent Page 必须是英文书写.如果是中文会出现找不到页面 500错误 2. 要在Page M ...

  2. HDU5407.CRB and Candies(数论)

    官方题解: The problem is just to calculate g(N) = LCM(C(N,0),C(N,1),...,C(N,N)) Introducing function f(n ...

  3. JS 创建对象的几种方式

    面向对象就是把属性和操作属性的方法放在一起作为一个相互依存的整体--对象,即拥有类的概念,基于类可以创建任意多个实例对象,一般具有封装.继承.多态的特性! ECMA-262把对象定义为:"无 ...

  4. js常用内置对象、Dom对象、BOM对象

    11.html元素事件属性中,如onclick="",双引号里可以是方法条用,可以是js代码(无需加<script>标签) 12.JavaScript内置 对象.属性和 ...

  5. IOS ScrollowView 滑动到边缘后不允许再拖动

    当scrollowview滑动图片时,滑动到最后一张图本应该不让其滑动,但是如果不可以去设置属性,依然可以滑动,露出白色的底色,挺影响美观的, 可以设置其属性: sv.bounces=NO; 这样就不 ...

  6. Entity Framework 教程(转)

    预备知识    2 LINQ技术    2 LINQ技术的基础 - C#3.0    2 自动属性    2 隐式类型    2 对象初始化器与集合初始化器    3 匿名类    3 扩展方法    ...

  7. 从BAE到SAE,从SAE又回到BAE

    版权声明:本文为博主原创文章,未经博主允许不得转载. [很久以后] 这段话是很久之后补充的,发现错误要勇于改正,以下红色字体是对以前观点的改正, 大概总结下: 1.bae最大缺点是需要备案,不过现在看 ...

  8. STM32F030 IO口外部中断应用

    //==文件exit.h============================================================ #ifndef __EXIT_H #define __ ...

  9. qt QSqlQuery

    QT数据库QSqlQuery   SQL执行操作 QSqlQuery提供了对数据库记录的Select.Insert.Update.Delete操作. SELECT操作: QSqlQuery query ...

  10. 【转】cocos2d-x获取系统时间——2013-08-25 10

    欢迎转载,本帖地址:http://blog.csdn.net/jinjian2009/article/details/9449585 之前使用过cocos2d-x获取系统时间,毫秒级的 long ge ...