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;
}