HashMap和HashSet

前言:

Map和Set都是一种用来专门进行搜索的容器或者数据结构,一般把搜索的关键字叫为Key,其对应的成为值,所组成的类型就是Key-Value模型,故模型就有两种

Map:Key-Value模型

Set:纯Key模型

HashMap

通过上图类之间的关系可知,Map这一集合与Collection集合分开,为单独存在的一个集合群,其顶级父类为Map类,我们本章要学的HashMap便是继承了此类

在HashMap中,其存放的key是唯一的,value可以重复多个,其与TreeMap的区别是:TreeMap中key值不能为null,而在HashMap中key值可以为null,其两的区别会在后面的博客中讲到

其底层实现结构为哈希桶

主要方法

1.get() : 返回key所对应的value值

2.put() : 设置key和对应的value

3.remove() :删除key对应的映射关系

4.keySet() : 返回所有key的不重复集合

5.Collection<V> values() : 返回可重复的value集合

6.containsKet(Object key) : 是否包含key

5.containsValue(Object value) : 是否包含value

代码演示

public class myHashMap {
    public static void main(String[] args) {
        HashMap<String,Integer> hashMap = new HashMap<>();

        //put方法
        hashMap.put("价格",23);
        hashMap.put("吃饭",55);
        hashMap.put("游泳",66);
        hashMap.put("睡觉",77);

        //get方法 -- 通过key获取value
        Integer a = hashMap.get("价格");
        System.out.println(a);

        //remove 删除key对应的映射关系
        hashMap.remove("价格");
        System.out.println(hashMap.containsKey("价格")); // false;

        //Collection<V> values() -- 返回可重复的value值
        Collection<Integer> collection = hashMap.values();
        System.out.println(collection); // 55 66 77

        //Set<V> keySet -- 返回不可重复的key值 放在Set集合里面
        Set<String> set = hashMap.keySet();
        System.out.println(set);

        //containsKey(Object key) 是否存在key值
        System.out.println(hashMap.containsKey("吃饭")); // true

        //containsValue(Object value)
        System.out.println(hashMap.containsValue("100"));//false;
    }
}

Map.Entry<K,V>的说明

Map.Entry<K,V>是Map内部实现的用来存储<key,value>键值对这种映射关系的内部类,其中设置了<key,value>的获取,value的设置

public static void main(String[] args) {
        HashMap<String,Integer> hashMap = new HashMap<>();
        hashMap.put("价格",23);
        hashMap.put("吃饭",55);
        hashMap.put("游泳",66);
        hashMap.put("睡觉",77);

        for (Map.Entry<String,Integer> entrySet:hashMap.entrySet()) {
            System.out.println(entrySet);
        }
    }


Set

通过上图的类继承关系,我们可以知道HashSet类继承Collection接口类

Set集合只存储key,且规定key一定要唯一

Set的最大功能就是对集合中的元素进行去重

Set的key不能修改,只能先删除,再插入新的

TreeSet不能插入null值,HashSet可以插入

HashSet底层结构是哈希桶

主要方法

add() : 添加元素 但重复元素不会被添加成功

clear() : 清空元素

remove(Object o) : 删除集合中的o

size() : 返回Set集合中的个数

Iterator<E> iterator() : 返回迭代器

Object[] toArray() : 将Set集合转换成数组返回

containsAll(Collection<> c) : 集合c中的元素在Set中是否全部存在

addAll(Collection<> c) : 添加集合c中所有元素,可以达到去重的效果

代码演示

 public static void main(String[] args) {
        HashSet<Integer> hashSet = new HashSet<>();
        Collection<Integer> collection = new HashSet<>();
        collection.add(2);
        collection.add(3);
        collection.add(4);
        collection.add(5);
        collection.add(6);
        collection.add(7);
        //add 添加元素 -- 如果相同则不会添加
        hashSet.add(1);
        hashSet.add(2);
        hashSet.add(3);
        hashSet.add(4);
        hashSet.add(5);
        hashSet.add(5);

        //remove 去掉指定目标key
        hashSet.remove(1);
        System.out.println(hashSet); // 2 3 4 5

        //size 返回当前set个数
        System.out.println(hashSet.size()); //4

        //转换成数组
        Object [] arr = hashSet.toArray();
        System.out.println(Arrays.toString(arr)); // 2 3 4 5

        //containsAll 集合中的值是否全部存在
        System.out.println(hashSet.containsAll(collection)); // true

        //addAll(Collection<> c) 添加集合中c的全部元素,会自动去重
        hashSet.addAll(collection);
        System.out.println(hashSet); // 2 3 4 5 6 7
    }

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值