Map<Key,Value>集合的共性方法:
K - 此映射所維護的鍵的類型
V - 映射值的類型
增
put(K key, V value)
putAll(Map<? extends K,? extends V> m)
刪
remove(Object key)
判斷
containsKey(Object key) 是否包含指定鍵
containsValue(Object value) 是否包含指定值
isEmpty()
查詢
entrySet() 返回此映射中包含的映射關系的 Set 視圖
get(Object key)
keySet() 返回此映射中包含的鍵的 Set 視圖。
### Map的常用子類:
|---HashTable: 底層是Hash表數據結構,不允許存入null鍵null值。線程同步的,JDK1.1,效率低
|---HashMap: 底層是Hash表數據結構,允許存入null鍵null值。線程不同步,JDK1.2,效率高(其余與HashTable基本相同)
|---TreeMap:底層是二叉樹數據結構,該映射根據其鍵的自然順序進行排序。或者根據創建映射時提供的 Comparator 進行排序 ,具體取決于使用的構造方法。
### Map集合的兩種遍歷方式:
~~~
import java.util.*;
public class MapDemo{
public static void main(String []args){
Map<String,String> map = new HashMap<String,String>();
map.put("01","lzl-1");
map.put("02","lzl-2");
map.put("03","lzl-3");
map.put("04","lzl-4");
map.put("05","lzl-4");
method_keySet(map);
method_entrySet(map);
}
public static void method_keySet(Map<String,String> map){
//通過keySet方法獲取Set<key>類型的值。
Set<String> keySet = map.keySet();
//有了Set集合,就可以用到Iterator(迭代器)了
Iterator<String> it = keySet.iterator();
while(it.hasNext()){
//獲取map集合中的key值
String key = it.next();
//通過Map集合中的get方法,獲取Value值
String value = map.get(key);
System.out.println("keySet方法--->"+" key:"+key+" value:"+value);
}
}
/*
*/
public static void method_entrySet(Map<String,String> map){
//通過entrySet方法獲取Map集合中的映射關系
Set<Map.Entry<String,String>> entrySet = map.entrySet();
//獲取Set集合,使用迭代器提供的方法獲取Map集合中存入的鍵值
Iterator<Map.Entry<String,String>> it = entrySet.iterator();
while(it.hasNext()){
//Entry是Map接口這種的內部靜態接口,實現該接口的方法,都擁有getValue()和getKey()的方法。
Map.Entry<String,String> me = it.next();
String value = me.getValue();
String key = me.getKey();
System.out.println("entrySet方法--->"+" key:"+key+" value:"+value);
}
}
}
~~~
### 例子:一個學生對象,包括姓名和年齡。每個學生都有家庭住址。
我們認為學生的姓名和年齡相同表示同一個人。地址可以相同
步驟:
1、定義一個學生類,重寫hashCode和equals方法。
并實現Comparable接口,重寫compareTo(Object o1,Object o2)方法按照年齡和姓名的順序來排列
2、將學生對象和學生的住址存入到HashMap中去。key--Student,value--String(地址)
3、通過keySet或者entrySet方法將數據一一取出。
解釋:
因為HashMap底層是hash表,每次new一個對象時,會產生不同的hashCode。因此不能比較Student中姓名和年齡相同的值。
所以需要覆寫hashCode和equals方法
實現Comparable接口,為了規范,可以使TreeMap集合存儲Student對象的數據。若不實現,則不能用TreeMap存儲。
~~~
import java.util.*;
//實現Comparable接口,提供按照姓名和年齡的自然順序排序的方法
class Student implements Comparable<Student>
{
private String name;
private int age;
public Student(String name,int age){
this.name = name;
this.age = age;
}
public int getAge(){
return age;
}
public String getName(){
return name;
}
public int compareTo(Student s){
int num = this.getName().compareTo(s.getName());
if(num == 0)
return new Integer(this.getAge()).compareTo(new Integer(s.getAge()));
return num;
}
public int hashCode(){
//保證new出的對象的hash值的唯一性
return name.hashCode()+age*60;
}
public boolean equals(Object obj){
if(!(obj instanceof Student))
throw new RuntimeException("類型不匹配");
Student stu = (Student)obj;
return this.age==stu.getAge() && this.name.equals(stu.getName());
}
public String toString(){
return "[name = "+name+" age = "+age+"]";
}
}
public class HashMapDemo{
public static void main(String args[]){
Map<Student,String> map = new HashMap<Student,String>();
map.put(new Student("lzl",18),"河南理工");
map.put(new Student("lzl1",18),"河南理工");
map.put(new Student("lzl2",18),"河南理工");
map.put(new Student("lzl3",18),"河南理工");
map.put(new Student("lzl4",18),"河南理工");
map.put(new Student("lzl4",18),"河南理工");
getInfo(map);
}
public static void getInfo(Map<Student,String> map){
Set<Map.Entry<Student,String>> entrySet = map.entrySet();
Iterator<Map.Entry<Student,String>> it = entrySet.iterator();
while(it.hasNext()){
Map.Entry<Student,String> me = it.next();
Student stu = me.getKey();
String address = me.getValue();
System.out.println("Student:"+stu+" address:"+address);
}
}
}
~~~