1,Map介紹
接口Map<K,V> K --此映射所維護的鍵的類型;V--映射值的類型。
public interface Map<K,V>:將鍵映射到值的對象。一個映射不能包含重複的鍵;每個鍵最多隻能映射到一個值。
Map
|-Hashtable:底層是哈希表數據結構,不可以存入null鍵null值。該集合是線程同步。JDK1.0以後,執行效率低。
|-HashMap:底層是哈希表數據結構,允許使用null鍵null值。該集合是不同步的。JDK1.2以後,執行效率高。
|-TreeMap:底層是二叉樹數據結構,線程不同步。可以用於給map集合中的鍵進行排序。
2,Map常用方法
(1)添加
V put(K key, V value):將指定的值與此映射中的指定鍵關聯(可選操作)。
void putAll(Map<? extends K,? extends V> m):從指定映射中將所有映射關係複製到此映射中(可選操作)。
(2)刪除
V remove(Object key): 如果存在一個鍵的映射關係,則將其從此映射中移除(可選操作)。
void clear():從此映射中移除所有映射關係(可選操作)。
(3)判斷
boolean containsKey(Object key):如果此映射包含指定鍵的映射關係,則返回 true。
boolean containsValue(Object value):如果此映射將一個或多個鍵映射到指定值,則返回 true。
boolean isEmpty():如果此映射未包含鍵-值映射關係,則返回 true。
(4)獲取
V get(Object key):返回指定鍵所映射的值;如果此映射不包含該鍵的映射關係,則返回 null。
int size():返回此映射中的鍵-值映射關係數。
Collection<V> values():返回此映射中包含的值的 Collection 視圖。
Set<Map.Entry<K,V>> entrySet():返回此映射中包含的映射關係的 Set 視圖。
Set<K> keySet():返回此映射中包含的鍵的 Set 視圖。
3,Map集合常用方法運行示例
import java.util.*;
public class MapDemo {
public static void main(String[] args) {
Map<Integer, String> hm = new HashMap<Integer, String>();
//添加元素,如果出現添加時,相同的鍵,那麼後添加的值會覆蓋原有鍵對應值。並且put方法會返回被覆蓋的值。
sop(hm.put(1,"wgafg"));
sop(hm.put(1,"wergerh"));
hm.put(2,"erytrty");
hm.put(3,"jrjkty");
sop("原集合:"+hm);
//刪除操作
//sop(hm.remove(2));
//獲取操作
sop(hm.get(2));//可以通過get方法的返回值來判斷一個鍵是否存在。通過返回null來判斷。
//判斷操作
sop(hm.containsKey(2));
sop(hm.containsValue("erytrty"));
Collection<String> coll = hm.values();
sop("全部Value值:"+coll);
sop("修改後集合:"+hm);
}
public static void sop(Object obj){
System.out.println(obj);
}
}
4,Map集合的兩種取出方式
Map集合的取出原理:將Map集合轉成Set集合,再通過迭代器取值。
(1)Set<k> keySet:將map中所有的鍵存入到Set集合,因爲set具備迭代器。迭代方式可以取出所有的key值,再根據get方法。獲取每一個鍵對應的值。
(2)Set<Map.Entry<k,v>> entrySet:將Map集合中的映射關係存入到了set集合中,而這個關係的數據類型就是:Map.Entry。
運行示例:
/*
每一個學生都有對應的歸屬地。
學生Student,地址String。
學生屬性:姓名,年齡。
注意:姓名和年齡相同的視爲同一個學生。
保證學生的唯一性。
*/
import java.util.*;
class Student implements Comparable<Student>{
private String name;
private int age;
Student(String name,int age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int compareTo(Student s){
int num = new Integer(this.age).compareTo(new Integer(s.age));
if(num==0){
return this.name.compareTo(s.name);}
return num;
}
public int hashCode(){
return name.hashCode()+age*3;
}
public boolean equals(Object o){
if(!(o instanceof Student))
throw new ClassCastException("類型不匹配!!!");
Student s = (Student)o;
return this.name.equals(s.name)&&this.age==s.age;
}
public String toString(){
return this.name+"::"+this.age;
}
}
public class MapTest {
public static void main(String[] args) {
HashMap<Student,String> hm = new HashMap<Student, String>();
hm.put(new Student("lisi--1",1),"上海");
hm.put(new Student("lisi--1",2),"澳門");
hm.put(new Student("lisi--2",2),"北京");
hm.put(new Student("lisi--3",3),"南京");
hm.put(new Student("lisi--4",4),"香港");
//第一種取值方式keySet
Set<Student> keySet = hm.keySet();
Iterator<Student> it = keySet.iterator();
while(it.hasNext()){
Student s = it.next();
String addr = hm.get(s);
sop(s+"...."+addr);
}
sop("********************");
//第二種取值方式entrySet
Set<Map.Entry<Student, String>> entrySet = hm.entrySet();
Iterator<Map.Entry<Student, String>> iter = entrySet.iterator();
while(iter.hasNext()){
Map.Entry<Student, String> m = iter.next();
Student s = m.getKey();
String addr = m.getValue();
sop(s+"----"+addr);
}
}
public static void sop(Object o){
System.out.println(o);
}
}
5,Map集合練習
/*
"sdfwgewgsdf"獲取該字符串中的字母出現的次數。
*/
import java.util.*;
public class MapTest2 {
public static void main(String[] args) {
TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
String s = "agadar@g$afgrtre";
tm = getCount(s,tm);
Set<Map.Entry<Character, Integer>> entrySet = tm.entrySet();
Iterator<Map.Entry<Character, Integer>> it = entrySet.iterator();
while(it.hasNext()){
Map.Entry<Character, Integer> m = it.next();
Character c = m.getKey();
Integer i = m.getValue();
System.out.print(c+"("+i+")");
}
}
public static TreeMap getCount(String s,TreeMap<Character,Integer> tm){
int count = 0;
for (int i = 0; i < s.length(); i++) {
if(!(s.charAt(i)>='a'&&s.charAt(i)<='z'||s.charAt(i)>='A'&&s.charAt(i)<='Z'))
continue;
if(tm.containsKey(s.charAt(i)))
count = tm.get(s.charAt(i));
count++;
tm.put(s.charAt(i),count);
count = 0;
}
return tm;
}
}