java 集合四 Map
Map集合
interface Map:
-
class HashMap
-
interface SortedMap
-
class TreeMap
-
Map接口特点:
-
用于存储任意键值对(Key - Value)
-
键:无序、无下标、不允许重复(唯一)
-
值:无序、无下标、允许重复
Map父接口
特点:存储一对数据(Key - Value),无序、无下标、键不可重复,值可重复
Map接口的使用
public class Demo1 {
public static void main(String[] args) {
//创建Map集合
Map<String,String> map = new HashMap<>();
//添加元素
map.put("cn","中国");
map.put("uk","英国");
map.put("usa","美国");
map.put("cn","zhongguo");//键重复添加,后面的会把前面的覆盖
System.out.println("元素个数"+map.size());
System.out.println(map.toString());
//删除
// map.remove("usa");
// System.out.println("删除之后:"+map.size());
//遍历
//方式一:使用keySet();
Set<String> keyset = map.keySet();
for (String s : keyset) {
System.out.println(s+"----"+map.get(s));
}
//也可以是
for (String s : map.keySet()) {
System.out.println(s+"----"+map.get(s));
}
//方式二:使用entrySet()方法
Set<Map.Entry<String, String>> entries = map.entrySet();
for (Map.Entry<String, String> entry : entries) {
System.out.println(entry.getKey()+"--"+entry.getValue());
}
//也可以是
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey()+"--"+entry.getValue());
}
//Map.Entry 为映射对 该种方法效率更高
//判断
System.out.println(map.containsKey("cn"));
System.out.println(map.containsValue("中国"));
}
}
Map集合的实现类
HashMap [重点]
-
JDK1.2版本,线程不安全,运行效率快;允许用NULL作为key或是value
-
存储结构:哈希表(数组+链表+红黑树)
Hashtable (不怎么用了)
-
JDK1.0版本,线程安全,运行效率慢;不允许NULL作为key或是value
Properties
-
Hashtable的子类,要求key和value都是String 通常用于配置文件的读取
TreeMap
-
实现了SortedMap接口(是Map的子接口),可以对key自动排序
HashMap使用
/**
* 使用key的hashcode和equals作为重复依据
*/
public class Demo1 {
public static void main(String[] args) {
//创建Map集合
HashMap<Student,String> students = new HashMap<Student,String>();
//添加元素
Student s1 = new Student("孙悟空",100);
Student s2 = new Student("猪八戒",101);
Student s3 = new Student("沙和尚",102);
students.put(s1,"北京");
students.put(s2,"上海");
students.put(s3,"杭州");
System.out.println("元素个数:"+students.size());
System.out.println(students.toString());
//删除
students.remove(s1);
//遍历
//使用keySet();
for (Student key : students.keySet()) {
System.out.println(key.toString()+"=="+students.get(key));
}
//使用entrySet();
for (Map.Entry<Student, String> entry : students.entrySet()) {
System.out.println(entry.getKey()+"=="+entry.getValue());
}
//判断
System.out.println(students.containsKey("s1"));
System.out.println(students.containsValue("杭州"));
}
}
源码分析:
1 static final int DEFAULT_INITIAL_CAPACITY = 1 <<4; //hashMap初始容量大小
2 static final int MAXIMUM_CAPACITY = 1 << 30 ; //hashmap的数组最大容量
3 static final float DEFAULT_LOAD_FACTOR = 0.75f;//默认加载因子
4 static final int TREEIFY_THRESHOLD = 8; //jdk1.8 当链表长度大于8时,调整成红黑树
5 static final int UNTREEIFY_THRESHOLD = 6;//jdk1.8当链表长度小于6时,调整成链表
6 static final int MIN_TREETFY_CAPACITY = 64;//jdk1.8 当链表长度大于8时,并且集合元素个数大于等于64时,调整成红黑树
7 transient Node<K, V>[] table; //哈希表中的数组
8 size; //元素个数
TreeMap使用
//要求:元素要实现Comparable接口
public class Demo1 {
public static void main(String[] args) {
//创建集合
TreeMap<Student, String> treeMap = new TreeMap<>();
//添加元素
Student s1 = new Student("孙悟空",100);
Student s2 = new Student("猪八戒",101);
Student s3 = new Student("沙和尚",102);
treeMap.put(s1,"北京");
treeMap.put(s2,"上海");
treeMap.put(s3,"杭州");
System.out.println("元素个数:"+treeMap.size());
System.out.println(treeMap.toString());
}
}
其余的删除、遍历、判断与HashMap类似;定制比较器与TreeSet类似。
附Student类:
public class Student implements Comparable<Student>{
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}