一、Map接口和常用方法
Map接口实现类的特点【很实用】
再JDK8中,
- Map和Collection并列存在。用于保存具有映射关系的数据:Key — Value
- Map中的key和value可以是任何引用数据类型,会封装到HashMap$Node对象中
- Map中的Key不允许重复,原因和HashSet一样
- Map中的value可以重复
- Mao中的key可以为null,value也可以为null,注意key的null只能有一个,value的null可以有多个
- 常用String类作为Map的key
- key和value之间存在单向一对一关系,即通过指定的key总能找到对应的value
Map接口的特点
Map存放数据的key-value示意图
一对k-v是放在一个Node中的,因为Node实现了Entry接口,也有说一对k-v就是一个Entry
注意和LinkedHashSet不同
这里实现的是Node实现了Map.Entry接口而LinkedHashSet是Entry继承了Node注意区别
public class Map01 {
@SuppressWarnings({"all"})
public static void main(String[] args) {
Map map = new HashMap();
map.put("no1","jack");
map.put("no2","rose");
Set set = map.entrySet();
for (Object obj : set) {
Map.Entry entry = (Map.Entry)obj;
System.out.println(entry.getKey()+"-"+entry.getValue());
}
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
System.out.println(next);
}
//k-v最后是HashMap$Node node = newNode(hash,key,value,null)
//k-v为了方便程序员遍历,还会创建 EntrySet集合,该集合存放的元素类型是Entry
//而一个Entry对象包含了k-v EntrySet<Entry<K,V>>
//entrySet中,定义的类型是Map.Entry,但是实际上存放的还是HashMap$Node
//这样子方便我们的遍历
//因为Entry里面有两个getKey和getValue方法
}
}
Map接口的常用方法
put()方法
和list集合的add方法一样,但要输入key和value
注意不同点是如果key相同会进行value替换
public class MapMethod01 {
@SuppressWarnings({"all"})
public static void main(String[] args) {
Map map = new HashMap();
map.put("jack",new Book("",12));
map.put("jack","12");//会替换上一个
map.put("rose","jack");
map.put(null,"rose");
map.put("marry",null);
System.out.println(map);
}
}
remove()
根据key删除value
public class MapMethod01 {
@SuppressWarnings({"all"})
public static void main(String[] args) {
Map map = new HashMap();
map.put("jack",new Book("",12));
map.put("jack","12");//会替换上一个
map.put("rose","jack");
map.put(null,"rose");
map.put("marry",null);
System.out.println(map);//{null=rose, marry=null, rose=jack, jack=12}
map.remove(null);
System.out.println(map);//{marry=null, rose=jack, jack=12}
}
}
get()
根据key来获得Value
public class MapMethod01 {
@SuppressWarnings({"all"})
public static void main(String[] args) {
Map map = new HashMap();
map.put("jack",new Book("",12));
map.put("jack","12");//会替换上一个
map.put("rose","jack");
map.put(null,"rose");
map.put("marry",null);
System.out.println(map);//{null=rose, marry=null, rose=jack, jack=12}
map.remove(null);
System.out.println(map);//{marry=null, rose=jack, jack=12}
System.out.println(map.get("rose"));//jack
}
}
size()
获取元素个数
isEmpty()
判断个数是否为0
clear()
清除
containskey()
查找键是否存在
Map接口的常用方法
第一种使用keySet()
public class MapMethod01 {
@SuppressWarnings({"all"})
public static void main(String[] args) {
Map map = new HashMap();
map.put("jack",new Book("",12));
map.put("jack","12");//会替换上一个
map.put("rose","jack");
map.put(null,"rose");
map.put("marry",null);
//遍历
//第一组先取出所有的Key,通过KValue
Set keyset = map.keySet();
//1.增强for
for (Object key :keyset) {
System.out.println(key+"-"+map.get(key));
}
//2,iterator迭代器
Iterator iterator = keyset.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
System.out.println(next+"-"+map.get(next));
}
}
}
第二种使用values()
较为简单但不会输出key
public class MapMethod01 {
@SuppressWarnings({"all"})
public static void main(String[] args) {
Map map = new HashMap();
map.put("jack",new Book("",12));
map.put("jack","12");//会替换上一个
map.put("rose","jack");
map.put(null,"rose");
map.put("marry",null);
//第二组方式把所有values取出
Collection values = map.values();
for (Object o :values) {
System.out.println(o);
}
Iterator iterator = values.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
System.out.println(next);
}
第三种使用entrySet()
用到向下转型
public class Map01 {
@SuppressWarnings({"all"})
public static void main(String[] args) {
Map map = new HashMap();
map.put("no1","jack");
map.put("no2","rose");
Set set = map.entrySet();
for (Object obj : set) {
Map.Entry entry = (Map.Entry)obj;
System.out.println(entry.getKey()+"-"+entry.getValue());
}
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
Map.Entry mapenter = (Map.Entry)next;
System.out.println(mapenter);
}
HashMap小结
HashTable
HashTable的基本介绍
HashTable的底层机制
底层有数组HashTable$Entry[] 初始化大小为11
加载因子还是0.75