【Java Map接口】

78 篇文章 0 订阅

Map整体结构图

在这里插入图片描述

常用方法

package com.yuzhenc.collection;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author: yuzhenc
 * @date: 2022-03-09 21:58:33
 * @desc: com.yuzhenc.collection
 * @version: 1.0
 */
public class Test17 {
    public static void main(String[] args) {
        /*
            增加:put(K key, V value)
            删除:clear() remove(Object key)
            修改:
            查看:entrySet() get(Object key) keySet() size() values()
            判断:containsKey(Object key) containsValue(Object value)
                equals(Object o) isEmpty()
        */
        //Map特点:无序,唯一
        Map<Integer,String> map = new HashMap<>();
        map.put(1,"Lili");
        map.put(2,"Ana");
        map.put(3,"Amy");
        System.out.println(map.put(4, "Sam"));//null
        System.out.println(map.put(2,"Nana"));//Ana
        System.out.println(map);//{1=Lili, 2=Nana, 3=Amy, 4=Sam}
        System.out.println(map.size());//4
        //map.clear();
        System.out.println(map.remove(2));//Nana
        System.out.println(map.remove(5));//null
        System.out.println(map.containsKey(3));//true
        System.out.println(map.containsValue("Amy"));//true
        System.out.println(map.isEmpty());//false
        Map<Integer,String> map1 = new HashMap<>();
        map1.put(1,"Lili");
        map1.put(3,"Amy");
        map1.put(4,"Sam");
        System.out.println(map==map1);//false
        System.out.println(map.equals(map1));//true
        System.out.println(map.get(2));//null
        System.out.println(map.get(3));//Amy
        //keySet()对集合中的key进行遍历查看
        Set<Integer> set = map.keySet();
        for (Integer i : set) {
            System.out.print(i+"\t");//1	3	4
        }
        System.out.println();
        //values()对集合中的value进行遍历查看
        Collection<String> collection = map.values();
        for (String string : collection) {
            System.out.print(string+"\t");//Lili	Amy	Sam
        }
        System.out.println();
        //get(Object key) keySet()
        for (Integer integer : set) {
            System.out.print(integer+"="+map.get(integer)+"\t");//1=Lili	3=Amy	4=Sam
        }
        System.out.println();
        //entrySet()
        Set<Map.Entry<Integer,String>> entries = map.entrySet();
        for (Map.Entry<Integer,String> mapEntry : entries) {
            System.out.print(mapEntry.getKey()+"="+mapEntry.getValue()+"\t");//1=Lili	3=Amy	4=Sam
        }
        System.out.println();
    }
}

TreeaMap

package com.yuzhenc.collection;

import java.util.Map;
import java.util.TreeMap;

/**
 * @author: yuzhenc
 * @date: 2022-03-09 22:29:38
 * @desc: com.yuzhenc.collection
 * @version: 1.0
 */
public class Test18 {
    public static void main(String[] args) {
        Map<Integer,String> map = new TreeMap<>();
        map.put(2,"Ana");
        map.put(1,"Lili");
        map.put(3,"Amy");
        System.out.println(map);//{1=Lili, 2=Ana, 3=Amy}

        //内部比较器
        Map<Cat,Integer> map1 = new TreeMap<>();
        map1.put(new Cat("Lili",18),10);
        map1.put(new Cat("Ana",19),20);
        map1.put(new Cat("Amy",20),30);
        map1.put(new Cat("Lili",18),40);
        map1.put(new Cat("Lili",19),50);
        map1.put(new Cat("Lili",17),60);
        System.out.println(map1);//{Cat{name='Lili', age=17}=60, Cat{name='Lili', age=18}=40, Cat{name='Ana', age=19}=20, Cat{name='Lili', age=19}=50, Cat{name='Amy', age=20}=30}

        //外部比较器
        Bijiao bijiao = new Bijiao();
        Map<Dog,Integer> map2 = new TreeMap<>(bijiao);
        map2.put(new Dog("Lili",18),10);
        map2.put(new Dog("Ana",19),20);
        map2.put(new Dog("Amy",20),30);
        map2.put(new Dog("Lili",18),40);
        map2.put(new Dog("Lili",19),50);
        map2.put(new Dog("Lili",17),60);
        System.out.println(map2);//{Dog{name='Amy', age=20}=30, Dog{name='Ana', age=19}=20, Dog{name='Lili', age=17}=60, Dog{name='Lili', age=18}=40, Dog{name='Lili', age=19}=50}
    }
}

HashMap源码

public class HashMap<K,V>
    extends AbstractMap<K,V> //【1】继承的AbstractMap中,已经实现了Map接口
        //【2】又实现了这个接口,多余,但是设计者觉得没有必要删除,就这么地了
    implements Map<K,V>, Cloneable, Serializable{
                
                
        //【3】后续会用到的重要属性:先粘贴过来:
    static final int DEFAULT_INITIAL_CAPACITY = 16;//哈希表主数组的默认长度
        //定义了一个float类型的变量,以后作为:默认的装填因子,加载因子是表示Hsah表中元素的填满的程度
        //太大容易引起哈西冲突,太小容易浪费  0.75是经过大量运算后得到的最好值
        //这个值其实可以自己改,但是不建议改,因为这个0.75是大量运算得到的
        static final float DEFAULT_LOAD_FACTOR = 0.75f;
        transient Entry<K,V>[] table;//主数组,每个元素为Entry类型
        transient int size;
        int threshold;//数组扩容的界限值,门槛值   16*0.75=12 
        final float loadFactor;//用来接收装填因子的变量
        
        //【4】查看构造器:内部相当于:this(16,0.75f);调用了当前类中的带参构造器
        public HashMap() {
        this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
    }
        //【5】本类中带参数构造器:--》作用给一些数值进行初始化的!
        public HashMap(int initialCapacity, float loadFactor) {
        //【6】给capacity赋值,capacity的值一定是 大于你传进来的initialCapacity 的 最小的 2的倍数
        int capacity = 1;
        while (capacity < initialCapacity)
            capacity <<= 1;
                //【7】给loadFactor赋值,将装填因子0.75赋值给loadFactor
        this.loadFactor = loadFactor;
                //【8】数组扩容的界限值,门槛值
        threshold = (int)Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);
                
                //【9】给table数组赋值,初始化数组长度为16
        table = new Entry[capacity];
                   
    }
        //【10】调用put方法:
        public V put(K key, V value) {
                //【11】对空值的判断
        if (key == null)
            return putForNullKey(value);
                //【12】调用hash方法,获取哈希码
        int hash = hash(key);
                //【14】得到key对应在数组中的位置
        int i = indexFor(hash, table.length);
                //【16】如果你放入的元素,在主数组那个位置上没有值,e==null  那么下面这个循环不走
                //当在同一个位置上放入元素的时候
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
            Object k;
                        //哈希值一样  并且  equals相比一样   
                        //(k = e.key) == key  如果是一个对象就不用比较equals了
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }
        modCount++;
                //【17】走addEntry添加这个节点的方法:
        addEntry(hash, key, value, i);
        return null;
    }
        
        //【13】hash方法返回这个key对应的哈希值,内部进行二次散列,为了尽量保证不同的key得到不同的哈希码!
        final int hash(Object k) {
        int h = 0;
        if (useAltHashing) {
            if (k instanceof String) {
                return sun.misc.Hashing.stringHash32((String) k);
            }
            h = hashSeed;
        }
                //k.hashCode()函数调用的是key键值类型自带的哈希函数,
                //由于不同的对象其hashCode()有可能相同,所以需对hashCode()再次哈希,以降低相同率。
        h ^= k.hashCode();
        // This function ensures that hashCodes that differ only by
        // constant multiples at each bit position have a bounded
        // number of collisions (approximately 8 at default load factor).
                /*
                接下来的一串与运算和异或运算,称之为“扰动函数”,
                扰动的核心思想在于使计算出来的值在保留原有相关特性的基础上,
                增加其值的不确定性,从而降低冲突的概率。
                不同的版本实现的方式不一样,但其根本思想是一致的。
                往右移动的目的,就是为了将h的高位利用起来,减少哈西冲突
                */
        h ^= (h >>> 20) ^ (h >>> 12);
        return h ^ (h >>> 7) ^ (h >>> 4);
    }
        //【15】返回int类型数组的坐标
        static int indexFor(int h, int length) {
                //其实这个算法就是取模运算:h%length,取模效率不如位运算
        return h & (length-1);
    }
        //【18】调用addEntry
        void addEntry(int hash, K key, V value, int bucketIndex) {
                //【25】size的大小  大于 16*0.75=12的时候,比如你放入的是第13个,这第13个你打算放在没有元素的位置上的时候
        if ((size >= threshold) && (null != table[bucketIndex])) {
                        //【26】主数组扩容为2倍
            resize(2 * table.length);
                        //【30】重新调整当前元素的hash码
            hash = (null != key) ? hash(key) : 0;
                        //【31】重新计算元素位置
            bucketIndex = indexFor(hash, table.length);
        }
                //【19】将hash,key,value,bucketIndex位置  封装为一个Entry对象:
        createEntry(hash, key, value, bucketIndex);
    }
        //【20】
        void createEntry(int hash, K key, V value, int bucketIndex) {
                //【21】获取bucketIndex位置上的元素给e
        Entry<K,V> e = table[bucketIndex];
                //【22】然后将hash, key, value封装为一个对象,然后将下一个元素的指向为e (链表的头插法)
                //【23】将新的Entry放在table[bucketIndex]的位置上
        table[bucketIndex] = new Entry<>(hash, key, value, e);
                //【24】集合中加入一个元素 size+1
        size++;
    }
    //【27】
        void resize(int newCapacity) {
        Entry[] oldTable = table;
        int oldCapacity = oldTable.length;
        if (oldCapacity == MAXIMUM_CAPACITY) {
            threshold = Integer.MAX_VALUE;
            return;
        }
                //【28】创建长度为newCapacity的数组
        Entry[] newTable = new Entry[newCapacity];
        boolean oldAltHashing = useAltHashing;
        useAltHashing |= sun.misc.VM.isBooted() &&
                (newCapacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);
        boolean rehash = oldAltHashing ^ useAltHashing;
                //【28.5】转让方法:将老数组中的东西都重新放入新数组中
        transfer(newTable, rehash);
                //【29】老数组替换为新数组
        table = newTable;
                //【29.5】重新计算
        threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
    }
        //【28.6】
        void transfer(Entry[] newTable, boolean rehash) {
        int newCapacity = newTable.length;
        for (Entry<K,V> e : table) {
            while(null != e) {
                Entry<K,V> next = e.next;
                if (rehash) {
                    e.hash = null == e.key ? 0 : hash(e.key);
                }
                                //【28.7】将哈希值,和新的数组容量传进去,重新计算key在新数组中的位置
                int i = indexFor(e.hash, newCapacity);
                                //【28.8】头插法
                e.next = newTable[i];//获取链表上元素给e.next
                newTable[i] = e;//然后将e放在i位置 
                e = next;//e再指向下一个节点继续遍历
            }
        }
    }
}

HashSet底层原理

public class HashSet<E>{
    //重要属性:
    private transient HashMap<E,Object> map;
    private static final Object PRESENT = new Object();
    //构造器:
    public HashSet() {
        map = new HashMap<>();//HashSet底层就是利用HashMap来完成的
    }
        
    public boolean add(E e) {
        return map.put(e, PRESENT)==null;
    }      
}

TreeMap源码

class Entry<K,V> implements Map.Entry<K,V> {
	K key;
	V value;
	Entry<K,V> left;
    Entry<K,V> right;
    Entry<K,V> parent;
    boolean color = BLACK;
}
public class TreeMap<K,V>{
   //重要属性:
    //外部比较器:
    private final Comparator<? super K> comparator;
    //树的根节点:
    private transient Entry<K,V> root = null;
    //集合中元素的数量:
    private transient int size = 0;
    //空构造器:
    public TreeMap() {
    	comparator = null;//如果使用空构造器,那么底层就不使用外部比较器
    }
    //有参构造器:
    public TreeMap(Comparator<? super K> comparator) {
    this.comparator = comparator;//如果使用有参构造器,那么就相当于指定了外部比较器
    }
        
    public V put(K key, V value) {//k,V的类型在创建对象的时候确定了
     //如果放入的是第一对元素,那么t的值为null
     Entry<K,V> t = root;//在放入第二个节点的时候,root已经是根节点了
             //如果放入的是第一个元素的话,走入这个if中:
     if (t == null) {
                     //自己跟自己比
         compare(key, key); // type (and possibly null) check
                     //根节点确定为root
         root = new Entry<>(key, value, null);
                     //size值变为1
         size = 1;
         modCount++;
         return null;
     }
                
     int cmp;
     Entry<K,V> parent;
     // split comparator and comparable paths
             //将外部比较器赋给cpr:
     Comparator<? super K> cpr = comparator;
	//cpr不等于null,意味着你刚才创建对象的时候调用了有参构造器,指定了外部比较器
    if (cpr != null) {
        do {
            parent = t;
            cmp = cpr.compare(key, t.key);//将元素的key值做比较
                            //cmp返回的值就是int类型的数据:
                            //要是这个值《0 =0  》0
            if (cmp < 0)
                t = t.left;
            else if (cmp > 0)
                t = t.right;
            else//cpm==0
                            //如果key的值一样,那么新的value替换老的value  但是key不变 因为key是唯一的
                return t.setValue(value);
        } while (t != null);
    }
	//cpr等于null,意味着你刚才创建对象的时候调用了空构造器,没有指定外部比较器,使用内部比较器
     else {
         if (key == null)
             throw new NullPointerException();
         Comparable<? super K> k = (Comparable<? super K>) key;
         do {
             parent = t;
             cmp = k.compareTo(t.key);//将元素的key值做比较
             if (cmp < 0)
                 t = t.left;
             else if (cmp > 0)
                 t = t.right;
             else
                 return t.setValue(value);
         } while (t != null);
     }
     Entry<K,V> e = new Entry<>(key, value, parent);
     if (cmp < 0)
         parent.left = e;
     else
         parent.right = e;
     fixAfterInsertion(e);
     size++;//size加1 操作
     modCount++;
     return null;
   }     
}

TreeSet源码

public class TreeSet<E> extends AbstractSet<E>
    implements NavigableSet<E>, Cloneable, java.io.Serializable{
    //重要属性:
    private transient NavigableMap<E,Object> m;
    private static final Object PRESENT = new Object();
    
    //在调用空构造器的时候,底层创建了一个TreeMap
    public TreeSet() {
            this(new TreeMap<E,Object>());
    }
    
    TreeSet(NavigableMap<E,Object> m) {
            this.m = m;
    }
    
    public boolean add(E e) {
       return m.put(e, PRESENT)==null;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sqlboy-yuzhenc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值