Map接口及HashMap的底层实现

1.Map接口


Map接口是1.2才有

2. HashMap


作为Map的主要实现类,线程不安全,但是效率高。1.2才有。可以存储null的key和value。

在jdk7之前,使用的是数组+链表

jdk8之后,为了提高效率,使用数组+链表+红黑树实现。

3.LinkedHashMap


是HashMap的子类,在HashMap的基础上加了一对指针,指向前一个后一个元素,形成双向链表的结构。 这样在遍历的时候就效率就比较高。所以在经常遍历的情况下就选用LinkedHashMap,其他情况差不多都使用HashMap.

LinkedHashMap都没有重写HashMap的put方法,它重写的是在put方法中要用到的newNode。还有一个重要的区别就是LinkedHashMap的元素叫做Entry,是继承了HashMap的Node。

    //LinkedHashMap中重写后的newNode
    Node<K,V> newNode(int hash, K key, V value, Node<K,V> e) {
   
        LinkedHashMap.Entry<K,V> p =
            new LinkedHashMap.Entry<>(hash, key, value, e);
        linkNodeLast(p);
        return p;
    }

    //HashMap中的newNode
    Node<K,V> newNode(int hash, K key, V value, Node<K,V> next) {
   
        return new Node<>(hash, key, value, next);
    }

    //LinkedHaspMap中的Entry
    static class Entry<K,V> extends HashMap.Node<K,V> {
   
        Entry<K,V> before, after;
        Entry(int hash, K key, V value, Node<K,V> next) {
   
            super(hash, key, value, next);
        }
    }

4. Hashtable


作为Map的古老实现类,线程安全,但是效率低,设计上有缺陷。1.0就出现,比Map接口还要早,不能存储null的key和value,这一点就体现类Hashtable不够健壮。这个同vector一样,我们也不再使用他,虽然他是线程安全的。

5. Properties


Hashtable的子类,主要用来处理配置文件。key和value都是String类型。我们一般将配置文件(.properties)读入内存的时候就是用Properties这个数据结构来存储。

具体使用如下

// 这里io流的处理方法不严谨
public static void main(String[] args) throws Exception{
   

        Properties prop = new Properties();
        FileInputStream files = new FileInputStream("test.properties");
        prop.load(files); // 将files对应的文件流加载到Properties对象中

        String name = prop.getProperty("name");
        String password = prop.getProperty("password");

        System.out.println(name+password);
        files.close();


    }

// test.properties文件内容如下,该配置文件是在当前工程目录下
name=luca
password=123

6. TreeMap


保证按照添加的Key-value对进行排序,但只会根据key排序,不会根据value排。所以一般都是key的自然排序和定制排序。因为有这个排序的特点,所以TreeMap的底层是用红黑二叉树实现。

虽然JDK8 后HashMap也是用红黑树实现的,但是HashMap要达到一定的条件后,才会将数组结构变为红黑树,有的场景需要你一开始就是排好序的,所以HashMap不能代替TreeMap

10.IdentityHashMap

Java中,有一种key值可以重复的map,就是IdentityHashMap。在IdentityHashMap中,判断两个键值k1和 k2相等的条件是 k1 == k2 。在正常的Map 实现(如 HashMap)中,当且仅当满足下列条件时才认为两个键 k1 和 k2 相等:(k1null ? k2null : e1.equals(e2))。

IdentityHashMap类利用哈希表实现 Map 接口,比较键(和值)时使用引用相等性代替对象相等性。该类不是 通用 Map 实现!此类实现 Map 接口时,它有意违反 Map 的常规协定,该协定在比较对象时强制使用 equals 方法。此类设计仅用于其中需要引用相等性语义的罕见情况。

7. jdk7中HashMap底层原理实现(数组+链表)

7.1 构造器


public HashMap(int initialCapacity, float loadFactor) {
   
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal initial capacity: " +
                                               initialCapacity);
        if (initialCapacity > MAXIMUM_CAPACITY)
            initialCapacity = MAXIMUM_CAPACITY;
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal load factor: " +
                                               loadFactor);

        this.loadFactor = loadFactor;
        threshold = initialCapacity;
        init();
    }

    /**
     * Constructs an empty <tt>HashMap</tt> with the specified initial
     * capacity and the default load factor (0.75).
     *
     * @param  initialCapacity the initial capacity.
     * @throws IllegalArgumentException if the initial capacity is negative.
     */
    public HashMap(int initialCapacity) {
   
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    }

    /**
     * Constructs an empty <tt>HashMap</tt> with the default initial capacity
     * (16) and the default load factor (0.75).
     */
    public HashMap() {
   
        this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
    }

    /**
     * Constructs a new <tt>HashMap</tt> with the same mappings as the
     * specified <tt>Map</tt>.  The <tt>HashMap</tt> is created with
     * default load factor (0.75) and an initial capacity sufficient to
     * hold the mappings in the specified <tt>Map</tt>.
     *
     * @param   m the map whose mappings are to be placed in this map
     * @throws  NullPointerException if the specified map is null
     */
    public HashMap(Map<? extends K, ? extends V> m) {
   
        this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
                      DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
        
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值