map & set 学习与使用

map & set 学习与使用

在这里插入图片描述
Map和set是一种专门用来进行搜索的容器或者数据结构,其搜索的效率与其具体的实例化子类有关。

举例:

  1. 根据姓名查询考试成绩
  2. 通讯录,即根据姓名查询联系方式
  3. 不重复集合,即需要先搜索关键字是否已经在集合中

一、map使用

Map是一个接口类,该类没有继承自Collection,该类存储 <Key,value>的键值对,即存储key与value的映射关系。key是唯一的,value可以重复。

(1) Map是一个接口,不能直接实例化对象,如果要实例化对象只能实例化其实现类TreeMap或者HashMap。如下所示:

Map<String,Integer> map=new HashMap<>();
Map<String,Integer> map1=new TreeMap<>();
SortedMap<String,Integer> map2=new TreeMap<>();
1.常用方法:

在这里插入图片描述

	Map<String,Integer> map=new HashMap<>();
    map.put("小明",80);
    map.put("张三",79);
    map.put("lili",84);

    System.out.println(map.get("张三"));
    System.out.println(map.get("李白"));
    System.out.println(map.getOrDefault("小明",0));
    System.out.println(map.getOrDefault("李白",0));

    System.out.println(map.containsKey("lili"));
    System.out.println(map.containsKey(100));

结果:79 null 80 0 true false

Set<String> key=map.keySet();
for(String s:key){
   System.out.println(s);
}

结果:张三 小明 lili

for(int n:map.values()){
  System.out.println(n);
}

结果:79 80 84

Set<Map.Entry<String, Integer>> entrySet=map.entrySet();
for (Map.Entry<String, Integer> entry:entrySet) {
 System.out.println(entry.getKey() + "," + entry.getValue());
 }

结果:张三,79 小明,80 lili,84

Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator();
while (it.hasNext()) {
     Map.Entry<String, Integer> entry = it.next();
     System.out.println("key:" + entry.getKey() + " value:" + entry.getValue());
        }

结果:key:张三 value:79 key:小明 value:80 key:lili value:84

2.删除操作:

(1)迭代器方法

for(Iterator<String> iterator = map.keySet().iterator();iterator.hasNext();){
   String name=iterator.next();
  if(name.equals("张三")) {
    iterator.remove();
  }
}

(2)removeIf方法 (版本8+)
removeIf是Collection s 的方法。一个Map本身不是一个Collection,也无法访问removeIf自己。通过values,keySet或entrySet 实现removeIf调用。

 //根据map中得值去判断删除
map.values().removeIf(value -> !value.contains(80));
 // 根据key删除
map.keySet().removeIf(key -> key == "lili");
 //通过getkey() getValue()方法获得值去删除
map.entrySet().removeIf(entry -> entry.getKey() == "lili");
map.entrySet().removeIf(entry -> entry.getValue() != 80);
3.注意:

1.Map是一个接口,不能直接实例化对象,如果要实例化对象只能实例化其实现类TreeMap或者HashMap
2. Map中存放键值对的Key是唯一的,value是可以重复的
3. 在Map中插入键值对时,key不能为空,否则就会抛NullPointerException异常,但是value可以为空
4. Map中的Key可以全部分离出来,存储到Set中来进行访问(因为Key不能重复)。
5. Map中的value可以全部分离出来,存储在Collection的任何一个子集合中(value可能有重复)。
6. Map中键值对的Key不能直接修改,value可以修改,如果要修改key,只能先将该key删除掉(重新覆盖),然后再来进行重新插入。
7. TreeMap和HashMap的区别

== TreeMap和HashMap的区别==
在这里插入图片描述

二、set使用

Set是一个接口类,该类没有继承自Collection,Set中只存储了key值,key是唯一的,set存储不重复集合。

Set<Integer> set=new HashSet<>();
Set<Integer> set1=new TreeSet<>();
set.add(9);
set.add(20);
set.add(20);
set.add(12);
1.常用方法

在这里插入图片描述在这里插入图片描述

        System.out.println(set.size());
        System.out.println(set.isEmpty());
        System.out.println(set.contains(34));
        System.out.println("============================");
        Iterator<Integer> it=set.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
        System.out.println("============================");

结果:
3 false false
20 9 12

        set.remove(12);
        Iterator<Integer> it1=set.iterator();
        while(it1.hasNext()){
            System.out.println(it1.next());
        }
        System.out.println("============================");

结果:20 9

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

结果:20 9 12

Collection<Integer> c=new ArrayList<>();
        c.add(10);
        c.add(20);
        c.add(13);

        System.out.println(set.containsAll(c));
        set.addAll(c);
        Iterator<Integer> it1=set.iterator();
        while(it1.hasNext()){
            System.out.println(it1.next());
        }

结果:false 20 9 10 12 13

2.注意:

  1. Set是继承自Collection的一个接口类
  2. Set中只存储了key,并且要求key一定要唯一
  3. Set的底层是使用Map来实现的,其使用key与Object的一个默认对象作为键值对插入到Map中的
  4. Set最大的功能就是对集合中的元素进行去重
  5. 实现Set接口的常用类有TreeSet和HashSet,还有一个LinkedHashSet,LinkedHashSet是在HashSet的基础
    上维护了一个双向链表来记录元素的插入次序。
  6. Set中的Key不能修改,如果要修改,先将原来的删除掉,然后再重新插入
  7. Set中不能插入null的key。
  8. TreeSet和HashSet的区别比

TreeSet和HashSet的区别比
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值