Map与Set详解

文章目录

Map

1.Map的定义

Map中存储的是Key-Value的键值对,将键映射到值的对象,映射不能包含重复的键,每个键最多可以映射到一个值

2.Map的常见方法

在这里插入图片描述

3.Map的注意事项

(1)put是根据一个函数来存放的,并非按顺序存放
(2)Map不能存放相同的键,如果存放,则会更新为最新的value
(3)Map不能使用迭代器进行打印
(4)Map中的key,value均可以为null
(5)Map是一个接口,不能直接实例化对象,只能实例化TreeMap或者HashMap
(6)Map中存放键值对的key是唯一的,value是可以重复的

4.代码示例

        HashMap<String,String> map = new HashMap<>();
        //1. put存放元素
        map.put("张三","java");
        map.put("李四","c++");
        map.put("王五","python");
        System.out.println(map);

        //2.get,获取key对应的value
        String str1 = map.get("王五");
        System.out.println(str1);

        //3.getOrDefault,返回key对应的value
        String str2 = map.getOrDefault("李四","php");
        System.out.println(str2);
        String str3 = map.getOrDefault("张田","js");
        System.out.println(str3);

        //4.remove,删除key对应的映射关系
        map.remove("王五");
        System.out.println(map);

        //5.再次put
        map.put("小明","k");
        map.put("李四","yu");
        map.put("小红","k");
        System.out.println(map);

        //6.keySet, 返回key的所有不重复集合
        Set<String> set = map.keySet();
        System.out.println(set);

        //7.values,返回所有value的可重复集合
        Collection<String> coll = map.values();
        System.out.println(coll);

        //8.entrySet,返回所有的keyValue映射
        Set<Map.Entry<String,String>> set2 = map.entrySet();
        for(Map.Entry<String,String> entry : set2){
            System.out.println(entry.getKey()+"  "+entry.getValue());
        }

        //9.containsKey
        System.out.println(map.containsKey("小红"));

        //10.containsValue
        System.out.println(map.containsValue("java"));

        //11.size
        System.out.println(map.size());

        //12.isEmpty
        System.out.println(map.isEmpty());

运行结果:
在这里插入图片描述

Set

1.Set的定义

Set是继承来自Collection的接口类,Set中存放了Key

2.Set的常用方法

在这里插入图片描述

3.Set的注意事项

(1)Set的底层使用Map来实现的,其使用key与Object的一个默认对象作为键值对插入到Map中
(2)Set最大的功能就是去重

4.代码示例

        HashSet<String> set = new HashSet<>();
        //1.add添加元素
        set.add("one");
        set.add("two");
        set.add("three");
        set.add("four");
        System.out.println(set);

        //2.contains,判断是否包含元素
        System.out.println(set.contains("three"));

        //3.返回迭代器
        Iterator<String> it = set.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }

        //4.删除集合中的元素
        System.out.println(set.remove("two"));
        System.out.println(set);

        //5.size,获取元素个数
        System.out.println(set.size());

        //6.isEmpty,判断是否为空
        System.out.println(set.isEmpty());

        //7.toArray
        Object [] array = set.toArray();
        for(int i = 0; i < array.length; i++){
            System.out.println(array[i]);
        }

        Collection<String> coll = new HashSet<>();
        coll.add("one");
        coll.add("three");
        //8.containsAll(Collection<?>)
        System.out.println(set.containsAll(coll));

        //9.addAll(Collection<? extends E> c)
        Collection<String> co = new HashSet<>();
        co.add("one");
        co.add("four");
        co.add("two");
        System.out.println(set.addAll(co));
        System.out.println(set);

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值