Java集合之Map和Set

时间:2025-02-16 12:35:56

以前就知道Set和Map是java中的两种集合,Set代表集合元素无序、不可重复的集合;Map是代表一种由多个key-value对组成的集合。然后两个集合分别有增删改查的方法。然后就迷迷糊糊地用着。突然在一个风雨交加的夜晚,感觉不能这样迷迷糊糊,得深入地去研究一下,于是去看了看源码(jdk1.8)。

1.Map源码。

/**
 * An object that maps keys to values.  A map cannot contain duplicate keys;
 * each key can map to at most one value.

*The Map interface provides three collection view, which
 * allow a map's contents to be viewed as a set of keys, collection of values,
 * or set of key-value mappings.

这是jdk源码中的对map这个接口的描述,大概意思是说这是一个键值对映射的对象,一个map中不能包含重复的键,每一个键最多映射一个值;map这个接口提供了三个集合视图,一个是关于key的set集合,一个是关于value的collection集合,还有一个是关于key-value映射关系的set集合。分别是以下几个集合对象。

Set<K> keySet();

Collection<V> values();

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

可以很明显地看出,map就是set的扩展。看了这个有什么用呢?用途很多,更加深入理解集合,你会被这些设计者(Josh Bloch)的思想所折服—当然这都比较扯淡。来点实际的,以上三种集合的大小都是一样的,因为key-value是一一对应的,所以你有三种方式来遍历map。这位兄台已经进行实验。

http://www.2cto.com/kf/201212/179013.html

public interface Map<K,V> {
    // Query Operations

这是jdk1.8中的Map接口的定义,可以发现map并没有继承collection,但是我之前在网上看了好多都说map也继承的collection,让我百思不解。

2.Set源码。

public interface Set<E> extends Collection<E> {

set才是真正地继承了collection接口,map只是在set的基础上的一个扩展。继承collection的还有List;

/**
 * A collection that contains no duplicate elements.  More formally, sets
 * contain no pair of elements <code>e1</code> and <code>e2</code> such that
 * <code>e1.equals(e2)</code>, and at most one null element.  As implied by
 * its name, this interface models the mathematical <i>set</i> abstraction.
set是数学中的集合的概念,正如名字所暗示的一样,java中的Set是对数学中set的抽象,Set中的元素是不能重复的,Set最多可含一个null元素;对于任意的非null元素e1和e2,都满足e1.equals(e2)==false. 并且在Set接口中,还有一些交集和并集的方法,如 addAll(Collection<? extends E> c);  containsAll(Collection<?> c);

(虽然集合号称存储的是Java对象,但实际上并不会真正将Java对象放在集合中,而是在集合中保留对象的引用)

3.hashMap和hashSet

(1)hashMap

hashMap是Map的一个具体实现。

public HashMap() {
        this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
    }

这是HashMap中的默认无参的构造方法,设置了DEFAULT_LOAD_FACTOR=0.75f, 还有带参数的构造方法,可以设置负载因子(一种时间和空间成本上的折衷),增大负载因子可以减少所占内存的开销,但是会增加查询数据的开销;get()和put()都会用到查询。

* Associates the specified value with the specified key in this map.
     * If the map previously contained a mapping for the key, the old
     * value is replaced.

   public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
    }

这是hashMap中的对于put方法的描述,如果元素重复,则会保留key,替换value;刚刚在介绍Map时提到了Map.Entry这个东西,在hashMap中,Node<K,V>实现了这个接口(static class Node<K,V> implements Map.Entry<K,V> ); 每个key-value都放在了Node<K,V>这个对象中,采用 Node<K,V>[] tab 数组的方式来保存key-value对;HashMap使用一种传说中的“Hash算法”来确定每个元素的存储位置, 调用key的hashCode()方法,通过返回值来确定每个元素的存储位置。同理,get方法也是如此。

(2)hashSet

/**
     * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
     * default initial capacity (16) and load factor (0.75).
     */
    public HashSet() {
        map = new HashMap<>();
    }

一看到默认的构造方法就什么都明白了,HashSet是基于HashMap实现的,只是封装了HashMap,源码中也是这样描述的。