JDK1.8源码笔记(14) Set&Map

Set

前言

* A collection that contains no duplicate elements.  More formally, sets
* contain no pair of elements e1 and e2 such that
* e1.equals(e2), and at most one null element.  As implied by
* its name, this interface models the mathematical set abstraction.
集合是一个不包含重复element的集合。set不存在两个element,使得e1.equals(e2)为true,而且最多只能有一个null。set就是对数学概念上的集合的抽象。

*

Note: Great care must be exercised if mutable objects are used as set
* elements.  The behavior of a set is not specified if the value of an object
* is changed in a manner that affects equals comparisons while the
* object is an element in the set.  A special case of this prohibition is
* that it is not permissible for a set to contain itself as an element.
如果加入Set的element是可变的,那就需要格外注意。

继承类&实现接口

继承Collection接口。

成员变量&成员方法

把Set内的方法和Collection内的方法比较一下可以看出,这些方法基本上都在Collection中定义过了。

Map

前言

* An object that maps keys to values.  A map cannot contain duplicate keys;
* each key can map to at most one value.
一个映射key到value的对象。一个map不能包含重复的key,每个key只能映射至多一个value。

*

This interface takes the place of the Dictionary class, which
* was a totally abstract class rather than an interface.
这个接口是用来替代Dictionary,Dictionary是一个抽象类而不是接口。

我们知道早在JDK1.0的时候,是没有Map的,只有HashTable的,HashTable就是继承Dictionary的,但是HashTable最大的问题,和Vector一样,是线程安全的。

*

The Map interface provides three collection views, which
* allow a map's contents to be viewed as a set of keys, collection of values,
* or set of key-value mappings.
Map提供三种查看数据的collection view,第一种是以Set形式查看所有key,以集合形式查看所有value(因为value是可以重复的),第三种也是以Set形式查看所有key-value对。

* The order 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 TreeMap class, make
* specific guarantees as to their order; others, like the HashMap
* class, do not.
Map的顺序和具体的实现是有关系的,比如TreeMap是有顺序的,而HashMap则没有顺序。

继承类&实现接口

Map虽然不继承Collection,但Map是定义在Java的Collection Framework中的。

成员变量&成员方法

int size();
boolean isEmpty();

这两个方法在Collection中也有定义。

boolean containsKey(Object key);
* More formally, returns true if and only if
* this map contains a mapping for a key k such that
* (key==null ? k==null : key.equals(k)).
如果参数key为null,并且存在一个key也为null,或存在一个key和参数key equals。

boolean containsValue(Object value);
和containsKey判断的规则相同,只是用来判断value。

V get(Object key);
根据参数key,返回对应的value。但是没有对应的value的时候,会返回null,但是要注意,value可能会允许null值,所以可以结合containsKey避免这种特殊情况。

V put(K key, V value);
把一个键值对放入Map中。如果map中已经存在相应的key了,那么value会被刷新。

V remove(Object key);
移除key对应的键值对,并且将value返回,如果没有相应的key,会返回null。

void putAll(Map m);
Copies all of the mappings from the specified map to this map。
把m都放入当前的Map中。? extends K的意思是,K本身,或K的任何子类。

void clear();
清空Map。

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.
这和Collection中的toArray可不一样,toArray返回的是一个新的数组。而这个Set,只是一个View,类似数据库层面上的view,这个Set和Map对Key的引用是同一套。
* If the map is modified
* while an iteration over the set is in progress (except through
* the iterator's own remove operation), the results of
* the iteration are undefined.  The set supports element removal,
* which removes the corresponding mapping from the map, via the
* Iterator.remove, Set.remove,
* removeAll, retainAll, and clear
* operations.  It does not support the add or addAll
* operations.
如果迭代set的过程中通过除了iterator.remove之外的方法修改set的话,迭代的结果会失去定义。返回的这个set是支持任何的删除和清空操作的,但是添加操纵不支持。

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 remove operation),
* the results of the iteration are undefined.  The collection
* supports element removal, which removes the corresponding
* mapping from the map, via the Iterator.remove,
* Collection.remove, removeAll,
* retainAll and clear operations.  It does not
* support the add or addAll operations.
和keySet的描述差不多,backed by啊,只能删不能加啊,还是要看具体实现吧。

Set> entrySet();

boolean equals(Object o);
* More formally, two maps m1 and
* m2 represent the same mappings if
* m1.entrySet().equals(m2.entrySet()).
两个map m1和m2的equals定义是由他们entrySet的equals定义的。

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
* entrySet() view.
返回这个Map的hash code。这个值的定义是map中的每个entry的hash code值之和。

Map.Entry

前言

首先说这是Map的一个内部接口,是default类型的。

* A map entry (key-value pair).  The Map.entrySet method returns
* a collection-view of the map, whose elements are of this class.  The
* only way to obtain a reference to a map entry is from the
* iterator of this collection-view.  These Map.Entry objects are
* valid only 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 setValue operation on the map entry.
这个就是用来真正访问Map内键值对的方式。使用entrySet这个方法来返回Entry形式的Key和Value。只有通过返回的这个set的iterator才能直接获取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
* remove operation), the results of this call are undefined.
返回对应的value,如果mapping被移除的话,结果无定义。

V setValue(V value);

boolean equals(Object o);

int hashCode();
* Returns the hash code value for this map entry.  The hash code
     * of a map entry e is defined to be:


*     (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
     *     (e.getValue()==null ? 0 : e.getValue().hashCode())
     *

* This ensures that e1.equals(e2) implies that
* e1.hashCode()==e2.hashCode() for any two Entries
* e1 and e2, as required by the general
* contract of Object.hashCode.
这里可以看到hashCode计算方法,是由Key和Value的值共同决定的。

你可能感兴趣的:(JDK1.8源码笔记(14) Set&Map)