Java Map的常用方法

1.V put(K key, V value)
向map集合中添加Key为key,Value为value的元素,当添加成功时返回null,否则返回value。
就是说Map集合中的Key是不能重复的,但是Map集合中的Value是可以重复。

Map<Integer, Integer> map = new HashMap<>();
map.put(1,1);

2.void putAll(Map<? extends K,? extends V> m)
向map集合中添加指定集合的所有元素

        Map<Integer, Integer> map = new HashMap<>();
        map.put(1, 1);
        map.put(2, 1);
        map.put(3, 1);
        Map<Integer, Integer> map2 = new HashMap<>();
        map2.putAll(map);//深拷贝
        Map<Integer, Integer> map3 = new HashMap<>(map);//map2等同于map3
        map.put(4, 1);
        map.put(1, 2);

结果:

3.void clear()
把map集合中所有的键值删除

map.clear();

4.boolean containsKey(Object key)
检出map集合中有没有包含Key为key的元素,如果有则返回true,否则返回false。

        Map<Integer, Integer> map = new HashMap<>();
        map.put(1, 1);
        map.put(2, 1);
        boolean a = map.containsKey(1);
        boolean b = map.containsKey(3);

结果:

5.boolean containsValue(Object value)
检出map集合中有没有包含Value为value的元素,如果有则返回true,否则返回false。

        Map<Integer, Integer> map = new HashMap<>();
        map.put(1, 1);
        map.put(2, 1);
        boolean a = map.containsValue(1);
        boolean b = map.containsValue(2);

结果:

6.Set<Map.Entry<K,V>> entrySet()
返回map到一个Set集合中,以map集合中的Key=Value的形式返回到set中。

        Map<Integer, Integer> map = new HashMap<>();
        map.put(1, 1);
        map.put(2, 1);
        Set<Map.Entry<Integer, Integer>> set = map.entrySet();
        //遍历map的方法
        for (Map.Entry<Integer, Integer> m : map.entrySet()) {
            System.out.print("key:" + m.getKey() + "\n" + "value:" + m.getValue() + "\n");
        }
        /*
        输出:key:1
            value:1
            key:2
            value:1
         */

7.boolean equals(Object o)
判断两个map集合的元素是否相同

        Map<Integer, Integer> map = new HashMap<>();
        map.put(1, 1);
        map.put(2, 1);
        Map<Integer, Integer> map2 = new HashMap<>(map);
        System.out.print(map.equals(map2));
        //输出true

8.V get(Object key)
根据map集合中元素的Key来获取相应元素的Value

        Map<Integer, Integer> map = new HashMap<>();
        map.put(1, 3);
        map.put(2, 4);
        int value = map.get(1);
        System.out.print(value);//输出3

9.boolean isEmpty()
检出map集合中是否有元素,如果没有则返回true,如果有元素则返回false

        Map<Integer, Integer> map = new HashMap<>();
        boolean a1 = map.isEmpty();//true
        map.put(1, 3);
        map.put(2, 4);
        boolean a2 = map.isEmpty();//false

10.Set keySet()
返回map集合中所有Key

        Map<Integer, Integer> map = new HashMap<>();
        map.put(1, 3);
        map.put(2, 4);
        Set<Integer> set = map.keySet();
        for (Integer i : set) {
            System.out.print(i);//输出12
        }

11.V remove(Object key)
删除Key为key值的元素

        Map<Integer, Integer> map = new HashMap<>();
        map.put(1, 3);
        map.put(2, 4);
        System.out.print("删除前:" + map);
        map.remove(1);
        System.out.print("删除后:" + map);
        //输出:删除前:{1=3, 2=4} 删除后:{2=4}

12.int size()
返回map集合中元素个数

        Map<Integer, Integer> map = new HashMap<>();
        map.put(1, 3);
        map.put(2, 4);
        int size = map.size();
        System.out.print(size);//输出2

13.Collection values()
返回map集合中所有的Value到一个Collection集合

        Map<Integer, Integer> map = new HashMap<>();
        map.put(1, 3);
        map.put(2, 4);
        Collection c = map.values();
        System.out.print(c);//输出:[3, 4]
posted @ 2020-03-26 14:28  Shaw_喆宇  阅读(536)  评论(0编辑  收藏  举报