map & set 学习与使用
Map和set是一种专门用来进行搜索的容器或者数据结构,其搜索的效率与其具体的实例化子类有关。
举例:
- 根据姓名查询考试成绩
- 通讯录,即根据姓名查询联系方式
- 不重复集合,即需要先搜索关键字是否已经在集合中
一、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.注意:
- Set是继承自Collection的一个接口类
- Set中只存储了key,并且要求key一定要唯一
- Set的底层是使用Map来实现的,其使用key与Object的一个默认对象作为键值对插入到Map中的
- Set最大的功能就是对集合中的元素进行去重
- 实现Set接口的常用类有TreeSet和HashSet,还有一个LinkedHashSet,LinkedHashSet是在HashSet的基础
上维护了一个双向链表来记录元素的插入次序。- Set中的Key不能修改,如果要修改,先将原来的删除掉,然后再重新插入
- Set中不能插入null的key。
- TreeSet和HashSet的区别比
TreeSet和HashSet的区别比