Map
一、为什么需要使用map?
Map存储的元素为键值对,通常称为key-value
而key是不允许重复的
二、掌握Map的常用方法
三、Map的方法及遍历
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* @author 作者 小张:
* @version 创建时间:2019年7月31日 上午10:42:11
* 类说明
* Map接口: 所有子孙类存储键值对
* 两个重要的实现类: HashMap,TreeMap
*
* HashMap存储特点:key无序,唯一
*
* 分析原理: 与HashSet一致,都是用hash算法存储
*
* 验证原理:
*/
public class HashMapTest {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("zs", 44);
map.put("zs", 66);
map.put("ls", 55);
System.out.println(map);//确定唯一后,后面的value会覆盖前面的value
System.out.println(map.isEmpty());
Map<String, Integer> map2 = new HashMap<>();
map2.putAll(map);
System.out.println(map2);
System.out.println(map2.remove("zs"));
System.out.println("============");
//遍历1
Set<String> set1 = map.keySet();
for(String a:set1){
System.out.println(map.get(a));
}
System.out.println("============");
//键值对遍历
Set<Entry<String, Integer>> set = map.entrySet();
Iterator<Entry<String, Integer>> it = set.iterator();
while (it.hasNext()) {
Entry<String, Integer> entry = (Entry<String, Integer>) it.next();
System.out.println(entry.getKey()+"-->"+entry.getValue());
}
}
}
四、HashMap的key存储自定义对象
import java.util.HashMap;
import java.util.Map;
/**
* @author 作者 张迁:
* @version 创建时间:2019年7月31日 上午11:42:41
* 类说明
* 验证原理:HashMap的key存储自定义对象
* 注意:此处key存自定义对象的目的只是用于验证原理;
* 后续使用上key基本都是存String对象,value存任意对象
*
* 如果没有重写hashCode和equals,那么调的是Object的,所以不同对象hash值不同,
* equals是比较地址也不相等--不能确定唯一性
*
* 解决:重写hashCode和equals
*/
public class HashMapTest2 {
public static void main(String[] args) {
Map<Student, Integer> map = new HashMap<Student, Integer>();
map.put(new Student("zs",21), 1);
map.put(new Student("ls",30), 2);
map.put(new Student("ww",10), 3);
map.put(new Student("ls",50), 4);
System.out.println(map);
}
}
五、TreeMap
public class TreeMapTest {
public static void main(String[] args) {
Map<String, Integer> map = new TreeMap<>();
map.put("aa", 1);
map.put("bb", 2);
map.put("cc", 3);
map.put("aa", 4);
System.out.println(map);
}
}
{aa=4, bb=2, cc=3}
六、TreeMap存储对象
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
/**
* @author 作者 :
* @version 创建时间:2019年7月31日 下午2:21:39
* 类说明
*
* 方式1:自然排序法------》自定义类实现Comparable接口
*
* 方式2:比较器法------》自定义实现比较器comparator类
* 方式3:匿名内部类
*/
public class TreeMapTest2 {
public static void main(String[] args) {
Map<Teacher, Integer> map = new TreeMap<Teacher, Integer>();
map.put(new Teacher("cang",30), 1);
map.put(new Teacher("chen",25), 1);
map.put(new Teacher("wang",30), 1);
map.put(new Teacher("cang",50), 1);
map.put(new Teacher("wang",30), 1);
System.out.println(map);
Map<Student, Integer> map2 = new TreeMap<Student, Integer>(new MyComparator());
map2.put(new Student("cang",30), 1);
map2.put(new Student("chen",25), 1);
map2.put(new Student("wang",30), 1);
map2.put(new Student("cang",50), 1);
map2.put(new Student("wang",30), 1);
System.out.println(map2);
//匿名内部类
Map<Student, Integer> map3 = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if(o1.getName().equals(o2.getName()))
return o1.getAge()-o2.getAge();
return o1.getName().compareTo(o2.getName());
}
});
map3.put(new Student("cang",30), 1);
map3.put(new Student("chen",25), 1);
map3.put(new Student("wang",30), 1);
map3.put(new Student("cang",50), 1);
map3.put(new Student("wang",30), 1);
System.out.println(map3);
}
}