Map接口

Map没有继承Collection接口。也就是说Map和Collection是2种不同的集合。Collection可以看作是(value)的集合,而Map可以看作是(key,value)的集合。
Map接口由Map的内容提供3种类型的集合视图,一组key集合,一组value集合,或者一组key-value映射关系的集合。
一.Map接口"public interface Map<K,V>"
K-Key-键 V-Value-值
将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射一个值。
此接口代替 Dictionary 类,后者完全是一个抽象类,而不是一个接口。
二.Map接口和Collection接口的不同
Map是双列的,Collection是单列的
Map的键唯一,Collection的子体系Set是唯一的
Map集合的数据结构针对键有效,跟值无关;Collection集合的数据结构是针对元素有效
三.Map集合的功能概述
1.添加功能
V put(K key,V value):添加元素。这个其实还有另一个功能?替换
如果键是第一次存储,就直接存储元素,返回null
如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值

        HashMap<Integer,String> hashMap = new HashMap<>();
        hashMap.put(1,"孙悟空");
        hashMap.put(2,"猪八戒");
        hashMap.put(1,"唐僧");
        Set<Map.Entry<Integer, String>> entries = hashMap.entrySet();
        for (Map.Entry<Integer, String> entry : entries) {
            Integer key = entry.getKey();
            String value = entry.getValue();
            System.out.print(key+value+"\t");//1唐僧 2猪八戒
        }

2.删除功能
void clear():移除所有的键值对元素

        HashMap<Integer,String> hashMap = new HashMap<>();
        hashMap.put(1,"孙悟空");
        hashMap.put(2,"猪八戒");
        hashMap.put(1,"唐僧");
        hashMap.clear();

V remove(Object key):根据键删除键值对元素,并把值返回

        HashMap<Integer,String> hashMap = new HashMap<>();
        hashMap.put(1,"孙悟空");
        hashMap.put(2,"猪八戒");
        hashMap.put(1,"唐僧");
        hashMap.remove(1);
        Set<Map.Entry<Integer, String>> entries = hashMap.entrySet();
        for (Map.Entry<Integer, String> entry : entries) {
            Integer key = entry.getKey();
            String value = entry.getValue();
            System.out.print(key+value+"\t");//2猪八戒
        }

3.判断功能
boolean containsKey(Object key):判断集合是否包含指定的键

        HashMap<Integer,String> hashMap = new HashMap<>();
        hashMap.put(1,"孙悟空");
        hashMap.put(2,"猪八戒");
        hashMap.put(1,"唐僧");
        hashMap.remove(1);
        boolean b = hashMap.containsKey(1);
        System.out.println(b);//false

boolean containsValue(Object value):判断集合是否包含指定的值

        HashMap<Integer,String> hashMap = new HashMap<>();
        hashMap.put(1,"孙悟空");
        hashMap.put(2,"猪八戒");
        hashMap.put(1,"唐僧");
        hashMap.remove(1);
        boolean b = hashMap.containsValue("猪八戒");
        System.out.println(b);

boolean isEmpty():判断集合是否为空

        HashMap<Integer,String> hashMap = new HashMap<>();
        hashMap.put(1,"孙悟空");
        hashMap.put(2,"猪八戒");
        hashMap.put(1,"唐僧");
        hashMap.remove(1);
        boolean b = hashMap.isEmpty();
        System.out.println(b);//false

4.获取功能
Set<Map.Entry<K,V>> entrySet(): 返回一个键值对的Set集合

        HashMap<Integer, String> hashMap = new HashMap<>();
        hashMap.put(1, "孙悟空");
        hashMap.put(2, "猪八戒");
        hashMap.put(1, "唐僧");
        Set<Map.Entry<Integer, String>> entries = hashMap.entrySet();
        for (Map.Entry<Integer, String> entry : entries) {
            String value = entry.getValue();
            System.out.print(value);//唐僧猪八戒
        }

V get(Object key):根据键获取值

        HashMap<Integer, String> hashMap = new HashMap<>();
        hashMap.put(1, "孙悟空");
        hashMap.put(2, "猪八戒");
        hashMap.put(1, "唐僧");
        String s = hashMap.get(1);
        System.out.println(s);//唐僧

Set keySet():获取集合中所有键的集合

        HashMap<Integer, String> hashMap = new HashMap<>();
        hashMap.put(1, "孙悟空");
        hashMap.put(2, "猪八戒");
        hashMap.put(1, "唐僧");
        for (Integer integer : hashMap.keySet()) {
            String s = integer.toString();
            System.out.print(s);//12
        }

Collection values():获取集合中所有值的集合

        HashMap<Integer, String> hashMap = new HashMap<>();
        hashMap.put(1, "孙悟空");
        hashMap.put(2, "猪八戒");
        hashMap.put(1, "唐僧");
        Collection<String> values = hashMap.values();
        for (String value : values) {
            String s = value.toString();
            System.out.print(s);//唐僧猪八戒
        }

hashmap,hashtable,treemap区别:
hashmap : 无序存放, 新的操作类, key不可以重复 , 内部结构是哈希表
hashtable: 无序存放, 旧的操作类, key不允许重复 ,内部结构是哈希表
treemap: 可以排序的map集合,按集合中的key排序,key不允许重复,内部结构是二叉树
HashMap和Hashtable的区别:
1.HashMap: 线程不安全,效率高.允许null值和null键
2.Hashtable: 线程安全 , 效率低.不允许null值和null键

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值