Map集合(HashMap,TreeMap)

Map

总想赢者必输,不怕输者必赢

首先对Map进行一个整体的理解。

查看API可以知道,Map 其实就是将键映射到值的对象,每个键最多映射到一个值。

Map 与Collection接口的不同:

Map

Collection

双列的 单列的
数据结构针对键有效,对值无效 数据结构针对元素有效

Map的功能

1.添加功能:V put (key,value) 添加功能还有一个功能就是替换,添加的时候如果键是第一次存储,就直接存储元素,返回NUll,

如果键不是第一次存在,就用值把以前的值替换掉。

2.删除功能:V remove(key):根据键删除值对的元素,并把值返回。

                     V clear() 移除所有的键值对元素。

3.判断功能:boolean containsKey(Object key):判断集合是否包含指定的键
                     boolean containsValue(Object value):判断集合是否包含指定的值
                     boolean isEmpty():判断集合是否为空

4.获取功能:Set<Map.Entry<K,V>> entrySet(): 返回一个键值对的Set集合
                     V get(Object key):根据键获取值
                     Set<K> keySet():获取集合中所有键的集合
                     Collection<V> values():获取集合中所有值的集合

5.长度功能:int size():返回集合中的键值对的对数

public class Demo {
    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<>();
        map.put("文章", "马伊利");
        map.put("陈思成", "佟丽娅");
        map.put("贾乃亮", "李小璐");
        map.put("宝宝", "蓉儿");
        map.put("武大", "金莲");
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for(Map.Entry<String, String> en:entries){
            String key = en.getKey();
            String value = en.getValue();
            System.out.print(key+"=="+value+"  ");
        }
    }
}

结果: 贾乃亮==李小璐  文章==马伊利  陈思成==佟丽娅  宝宝==蓉儿  武大==金莲 

HashMap

HashMap允许插入null值null键。

HashMapd的唯一性依旧是重写hashCode()方法和equals()方法。

public class Demo {
    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<>();
        map.put("文章", "马伊利");
        map.put("陈思成", "佟丽娅");
        map.put("贾乃亮", "李小璐");
        map.put("宝宝", "蓉儿");
        map.put("武大", "金莲");
        map.put("宝宝", "蓉儿");
        map.put("武大", "金莲");
        Set<String> keys = map.keySet();
        for(String key:keys){
            String value = map.get(key);
            System.out.print(key+"==="+value+"  ");
        }
    }
}

结果:贾乃亮===李小璐  文章===马伊利  陈思成===佟丽娅  宝宝===蓉儿  武大===金莲 

HashMap与hashtable的区别:

HashMap:线程不安全,效率高,允许null值null键

Hashtable:线程安全,效率低,不允许null值null键

LinkedHashMap

概述:Map 接口的哈希表和链接列表实现,具有可预知的迭代顺序。

底层的数据结构是链表和哈希表 元素有序 并且唯一元素的有序性由链表数据结构保证

唯一性由 哈希表数据结构保证。

public class Demo {
    public static void main(String[] args) {
        LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
        map.put(100, "abc");
        map.put(200, "ccc");
        map.put(300, "ddd");
        Set<Map.Entry<Integer, String>> entries = map.entrySet();
        for (Map.Entry<Integer, String> entry : entries) {
            Integer key = entry.getKey();
            String value = entry.getValue();
            System.out.print(key+"==="+value+"  ");
        }
    }
}

结果: 100===abc  200===ccc  300===ddd

TreeMap

TreeMap键不允许插入null值

键的数据结构是红黑树,可保证键的排序和唯一性  
排序分为自然排序和比较器排序
线程是不安全的效率比较高
 

public class Demo {
    public static void main(String[] args) {
        TreeMap<Integer, Student> map = new TreeMap<>();
        map.put(10, new Student("钟楚红", 23));
        map.put(9, new Student("梅艳芳", 27));
        map.put(1, new Student("李丽珍", 26));
        map.put(4, new Student("翁虹", 27));
        map.put(2, new Student("叶子楣", 29));
        map.put(3, new Student("叶子楣222", 29));
        for (Integer integer : map.keySet()) {
            System.out.print(integer+"=="+map.get(integer)+"  ");
        }
    }
}

结果:1==Student{name='李丽珍', age=26}  2==Student{name='叶子楣', age=29}  3==Student{name='叶子楣222', age=29}  4==Student{name='翁虹', age=27}  9==Student{name='梅艳芳', age=27}  10==Student{name='钟楚红', age=23}

猜你喜欢

转载自blog.csdn.net/We_chuan/article/details/83895152