Java集合Map

Map

一个将键映射到值的对象,一个映射不能包含重复的键,每个健最多映射到一个值

Map接口的一些共性方法

添加

put(K key, V value)
putAll(Map<? extends K, ? extends V> m)

删除

clear();//清空
remove(Object key)

判断

containsValue(Object value)
containsKey(Object key)
isEmpty()

获取

get(Object key);
size()
values()
entrySet()
keySet();

HashMap

底层是哈希表数据结构,允许使用Null键和Null值,且该集合是不同步的,所以效率高

HashTable

底层是哈希表的数据结构,不允许存入null健null值,该集合是同步的,所以效率低

TreeMap

底层是二叉树数据结构,线程不同步,可以用于map集合中的健进行排序

增、删、判断的操作
public class Test {

    public static void main(String[] args) {
        Map<String,String> map = new HashMap();
        map.put("001","lisi");
        map.put("002","zhangsan");
        map.put("003","wanger");
        System.out.println(map);
        System.out.println(map.containsValue("lisi"));
        System.out.println(map.containsKey("001"));
        map.remove("001");
        System.out.println(map);
    }
}

在这里插入图片描述

在增里面,有一个特殊点就是,map不能存两个相同的值,即不能存在两个相等的 key 值

V put(K key, V value);	//map接口里面put方法的源码。我们可看到,它返回了一个值,一个value值

即,每当存入一个key时,都会返回一个value值,而这个value值就是这个原来key对应的value值,如果这个key值第一次添加进来,那么那么key原来对应的value值就是null了,但是如果存在key了那么就会返回之前的value值

public class Test {

    public static void main(String[] args) {
        Map<String,String> map = new HashMap();
        System.out.println(map.put("001","lisi01"));
        System.out.println(map.put("001","lisi02"));
        System.out.println(map);
    }
}

在这里插入图片描述

我们可以看到,第一次返回输出的是null,而第二次就是第一次放入的值了

获取方法get(Object key)、size()、values()
public class Test {

    public static void main(String[] args) {
        Map<String,String> map = new HashMap();
        map.put("001","lisi");
        map.put("002","zhangsan");
        map.put("003","wanger");
        System.out.println(map.get("002"));
        System.out.println(map.size());
        Collection<String> coll = map.values();
        for(String str :coll){
            System.out.println(str);
        }
    }
}

在这里插入图片描述

我们可以知道values返回的是一个Collection的类型,就把map里面所有的value值都装进去了

获取方法keySet()

在map里面是没有迭代器的,但是给我们提供了一个keySet()方法

Set<K> keySet();

这个方法的返回值是一个Set,所以通过keySet()方法就可以获取所有健的Set集合

public class Test {

    public static void main(String[] args) {
        Map<String,String> map = new HashMap();
        map.put("001","lisi");
        map.put("002","zhangsan");
        map.put("003","wanger");
        Set<String> keySet = map.keySet();
        Iterator<String> it = keySet.iterator();
        while (it.hasNext()){
            String key = it.next();
            System.out.println(key + "=" + map.get(key));
        }
    }
}

在这里插入图片描述
Map集合取出的原理:将map集合转换成Set集合,再通过迭代器取

这个时候,我们就通过keySet()获取到了map的所有key值,这个是否就方便我们对map进行具体的操作了,而不再是只能便利map,而是可以便利获得里面具体的值,且能操作

在这里插入图片描述

获取方法entrySet()

返回此映射中包含映射关系的Set视图
在这里我们又引出了一个Map.Entry<K,V>,它是Map的一个内部静态接口,源码如下,它提供了我们熟悉的 getKey(); getValue() 方法,这样方便我们更容易的获取到key value的值

interface Entry<K,V> {
    K getKey();
    V getValue();
    V setValue(V value);
    boolean equals(Object o);
    int hashCode();
    public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> comparingByKey() {
        return (Comparator<Map.Entry<K, V>> & Serializable)
                (c1, c2) -> c1.getKey().compareTo(c2.getKey());
    }
    public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue() {
        return (Comparator<Map.Entry<K, V>> & Serializable)
                (c1, c2) -> c1.getValue().compareTo(c2.getValue());
    }
    public static <K, V> Comparator<Map.Entry<K, V>> comparingByKey(Comparator<? super K> cmp) {
        Objects.requireNonNull(cmp);
        return (Comparator<Map.Entry<K, V>> & Serializable)
                (c1, c2) -> cmp.compare(c1.getKey(), c2.getKey());
    }
    public static <K, V> Comparator<Map.Entry<K, V>> comparingByValue(Comparator<? super V> cmp) {
        Objects.requireNonNull(cmp);
        return (Comparator<Map.Entry<K, V>> & Serializable)
                (c1, c2) -> cmp.compare(c1.getValue(), c2.getValue());
    }
}
public class Test {

    public static void main(String[] args) {
        Map<String, String> map = new HashMap();
        map.put("001", "lisi");
        map.put("002", "zhangsan");
        map.put("003", "wanger");
        Set<Map.Entry<String, String>> entries = map.entrySet();
        Iterator<Map.Entry<String, String>> it = entries.iterator();
        while (it.hasNext()){
            Map.Entry<String,String> me = it.next();
            String key = me.getKey();
            String value = me.getValue();
            System.out.println(key + "=" + value);
        }
    }
}

在这里插入图片描述

这样我们,依然可以对map进行遍历,获取具体的值
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值