Java類集--Map接口、HashMap、IdentityHashMap、SortedMap
Collection的操作中之前已經發現,每次保存的對象都是一個對象,但是在Map中保存的是一對對象,對象的形式是以:key->value的形式保存的。就好像電話本:張三-->12445
Map接口中的方法


Map接口中常用的子類:

import java.util.HashMap ;
import java.util.Map ;
public class HashMapDemo01{
public static void main(String args[]){
Map<String,String> map = null; // 聲明Map對象,其中key和value的類型為String
map = new HashMap<String,String>() ;
map.put("mldn","www.mldn.cn") ; // 增加內容
map.put("zhinangtuan","www.zhinangtuan.net.cn") ; // 增加內容
map.put("mldnjava","www.mldnjava.cn") ; // 增加內容
String val = map.get("mldn") ; // 根據key取出值
System.out.println("取出的內容是:" + val) ;
}
};
在map中使用containsXxx()方法判斷指定的key或value是否存在。
import java.util.HashMap ;
import java.util.Map ;
public class HashMapDemo02{
public static void main(String args[]){
Map<String,String> map = null; // 聲明Map對象,其中key和value的類型為String
map = new HashMap<String,String>() ;
map.put("mldn","www.mldn.cn") ; // 增加內容
map.put("zhinangtuan","www.zhinangtuan.net.cn") ; // 增加內容
map.put("mldnjava","www.mldnjava.cn") ; // 增加內容
if(map.containsKey("mldn")){ // 判斷key是否存在
System.out.println("搜索的key存在!") ;
}else{
System.out.println("搜索的key不存在!") ;
}
if(map.containsValue("www.mldn.cn")){ // 判斷value是否存在
System.out.println("搜索的value存在!") ;
}else{
System.out.println("搜索的value不存在!") ;
}
}
};

import java.util.HashMap ;
import java.util.Map ;
import java.util.Iterator ;
import java.util.Set ;
public class HashMapDemo03{
public static void main(String args[]){
Map<String,String> map = null; // 聲明Map對象,其中key和value的類型為String
map = new HashMap<String,String>() ;
map.put("mldn","www.mldn.cn") ; // 增加內容
map.put("zhinangtuan","www.zhinangtuan.net.cn") ; // 增加內容
map.put("mldnjava","www.mldnjava.cn") ; // 增加內容
Set<String> keys = map.keySet() ; // 得到全部的key
Iterator<String> iter = keys.iterator() ;
while(iter.hasNext()){
String str = iter.next() ;
System.out.print(str + "、") ;
}
}
};

import java.util.HashMap ;
import java.util.Map ;
import java.util.Iterator ;
import java.util.Collection ;
public class HashMapDemo04{
public static void main(String args[]){
Map<String,String> map = null; // 聲明Map對象,其中key和value的類型為String
map = new HashMap<String,String>() ;
map.put("mldn","www.mldn.cn") ; // 增加內容
map.put("zhinangtuan","www.zhinangtuan.net.cn") ; // 增加內容
map.put("mldnjava","www.mldnjava.cn") ; // 增加內容
Collection<String> values = map.values() ; // 得到全部的value
Iterator<String> iter = values.iterator() ;
while(iter.hasNext()){
String str = iter.next() ;
System.out.print(str + "、") ;
}
}
};
在Map中也存在一個Hashtable子類,實際上這個子類和Vector是一樣的,都屬於舊的類。
import java.util.HashMap ;
import java.util.Map ;
import java.util.Set ;
import java.util.Iterator ;
import java.util.Collection ;
public class HashtableDemo01{
public static void main(String args[]){
Map<String,String> map = null; // 聲明Map對象,其中key和value的類型為String
map = new HashMap<String,String>() ;
map.put("mldn","www.mldn.cn") ; // 增加內容
map.put("zhinangtuan","www.zhinangtuan.net.cn") ; // 增加內容
map.put("mldnjava","www.mldnjava.cn") ; // 增加內容
System.out.print("全部的key:") ;
Set<String> keys = map.keySet() ; // 得到全部的key
Iterator<String> iter = keys.iterator() ;
while(iter.hasNext()){
String str = iter.next() ;
System.out.print(str + "、") ;
}
System.out.print("\n全部的value:") ;
Collection<String> values = map.values() ; // 得到全部的value
Iterator<String> iter2 = values.iterator() ;
while(iter2.hasNext()){
String str = iter2.next() ;
System.out.print(str + "、") ;
}
}
};
HashMap與Hashtalbe的區別

在Map中還存在一個TreeMap的子類,此類也屬於排序類,按key排序。
import java.util.TreeMap ;
import java.util.Map ;
import java.util.Set ;
import java.util.Iterator ;
import java.util.Collection ;
public class TreeMapDemo01{
public static void main(String args[]){
Map<String,String> map = null; // 聲明Map對象,其中key和value的類型為String
map = new TreeMap<String,String>() ;
map.put("A、mldn","www.mldn.cn") ; // 增加內容
map.put("C、zhinangtuan","www.zhinangtuan.net.cn") ; // 增加內容
map.put("B、mldnjava","www.mldnjava.cn") ; // 增加內容
Set<String> keys = map.keySet() ; // 得到全部的key
Iterator<String> iter = keys.iterator() ;
while(iter.hasNext()){
String str = iter.next() ;
System.out.println(str + " --> " + map.get(str)) ; // 取出內容
}
}
};
使用TreeMap可以方便的完成排序的操作。如果自定義的類要想做為key的話,則肯定要實現Comparable接口,指定比較的規則。
弱引用類:WeakHashMap

如果假設一個Map中的某些內容長時間不使用的話,按照之前的做法是不會刪除的,如果希望其可以自動刪除掉,可以使用弱引用。當裏麵的某些內容不使用時,可以自動刪除掉。
import java.util.WeakHashMap ;
import java.util.Map ;
import java.util.Set ;
import java.util.Iterator ;
import java.util.Collection ;
public class WeakHashMapDemo01{
public static void main(String args[]){
Map<String,String> map = null; // 聲明Map對象,其中key和value的類型為String
map = new WeakHashMap<String,String>() ;
map.put(new String("mldn"),new String("www.mldn.cn")) ;
map.put(new String("zhinangtuan"),new String("www.zhinangtuan.net.cn")) ;
map.put(new String("mldnjava"),new String("www.mldnjava.cn")) ;
System.gc() ; // 強製性進行垃圾的收集操作
map.put(new String("lxh"),new String("lixinghua")) ;
System.out.println(map) ;
}
};

Map接口輸出:

Map接口一般隻作為查找使用,輸出的操作畢竟屬於少數。
import java.util.HashMap ;
import java.util.Map ;
import java.util.Set ;
import java.util.Iterator ;
public class IteratorDemo04{
public static void main(String args[]){
Map<String,String> map = null; // 聲明Map對象,其中key和value的類型為String
map = new HashMap<String,String>() ;
map.put("mldn","www.mldn.cn") ; // 增加內容
map.put("zhinangtuan","www.zhinangtuan.net.cn") ; // 增加內容
map.put("mldnjava","www.mldnjava.cn") ; // 增加內容
Set<Map.Entry<String,String>> allSet = null ;
allSet = map.entrySet() ;
Iterator<Map.Entry<String,String>> iter = null ;
iter = allSet.iterator() ;
while(iter.hasNext()){
Map.Entry<String,String> me = iter.next() ;
System.out.println(me.getKey() + " --> " + me.getValue()) ;
}
}
};第一種方式,使用Iterator完成。當然也可以使用foreach完成。import java.util.HashMap ;
import java.util.Map ;
import java.util.Set ;
import java.util.Iterator ;
public class ForeachDemo02{
public static void main(String args[]){
Map<String,String> map = null; // 聲明Map對象,其中key和value的類型為String
map = new HashMap<String,String>() ;
map.put("mldn","www.mldn.cn") ; // 增加內容
map.put("zhinangtuan","www.zhinangtuan.net.cn") ; // 增加內容
map.put("mldnjava","www.mldnjava.cn") ; // 增加內容
for(Map.Entry<String,String> me:map.entrySet()){
System.out.println(me.getKey() + " --> " + me.getValue()) ;
}
}
};

import java.util.Map ;
import java.util.HashMap ;
class Person{
private String name ;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
public String toString(){
return "姓名:" + this.name + ";年齡:" + this.age ;
}
};
public class HashMapDemo05{
public static void main(String args[]){
Map<String,Person> map = null ;
map = new HashMap<String,Person>() ;
map.put("zhangsan",new Person("張三",30)); // 增加內容
System.out.println(map.get("zhangsan")) ;
}
};如果現在以String為key是可以去除內容的。import java.util.Map ;
import java.util.HashMap ;
class Person{
private String name ;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
public String toString(){
return "姓名:" + this.name + ";年齡:" + this.age ;
}
};
public class HashMapDemo06{
public static void main(String args[]){
Map<Person,String> map = null ;
map = new HashMap<Person,String>() ;
map.put(new Person("張三",30),"zhangsan"); // 增加內容
System.out.println(map.get(new Person("張三",30))) ;
}
};此時,隻是將自定義的類作為key,但是在取值的時候發現取不了了,返回的結果是null,那麼為什麼之前的String可以,但是自定義的類不存在呢?實際上,對於匹配過程來講,有一個特點:即:對象要一樣才可以將內容查詢出來。
import java.util.Map ;
import java.util.HashMap ;
class Person{
private String name ;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
public String toString(){
return "姓名:" + this.name + ";年齡:" + this.age ;
}
};
public class HashMapDemo07{
public static void main(String args[]){
Map<Person,String> map = null ;
map = new HashMap<Person,String>() ;
Person per = new Person("張三",30) ;
map.put(per,"zhangsan"); // 增加內容
System.out.println(map.get(per)) ;
}
};

import java.util.Map ;
import java.util.HashMap ;
class Person{
private String name ;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
public String toString(){
return "姓名:" + this.name + ";年齡:" + this.age ;
}
public boolean equals(Object obj){
if(this==obj){
return true ;
}
if(!(obj instanceof Person)){
return false ;
}
Person p = (Person)obj ;
if(this.name.equals(p.name)&&this.age==p.age){
return true ;
}else{
return false ;
}
}
public int hashCode(){
return this.name.hashCode() * this.age ;
}
};
public class HashMapDemo08{
public static void main(String args[]){
Map<Person,String> map = null ;
map = new HashMap<Person,String>() ;
map.put(new Person("張三",30),"zhangsan"); // 增加內容
System.out.println(map.get(new Person("張三",30))) ;
}
};作為key,或者更準確的說是作為對象的時候,實際上是依靠hashCode()和equals()來判斷兩個匿名對象是否相等,這一點由係統內部自動完成。IdentityHashMap類
在正常的Map操作中,key本身是不能夠重複的。
import java.util.HashMap ;
import java.util.Set ;
import java.util.Iterator ;
import java.util.Map ;
class Person{
private String name ;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
public boolean equals(Object obj){
if(this==obj){
return true ;
}
if(!(obj instanceof Person)){
return false ;
}
Person p = (Person)obj ;
if(this.name.equals(p.name)&&this.age==p.age){
return true ;
}else{
return false ;
}
}
public int hashCode(){
return this.name.hashCode() * this.age ;
}
public String toString(){
return "姓名:" + this.name + ",年齡:" + this.age ;
}
};
public class IdentityHashMapDemo01{
public static void main(String args[]){
Map<Person,String> map = null ; // 聲明Map對象
map = new HashMap<Person,String>() ;
map.put(new Person("張三",30),"zhangsan_1") ; // 加入內容
map.put(new Person("張三",30),"zhangsan_2") ; // 加入內容
map.put(new Person("李四",31),"lisi") ; // 加入內容
Set<Map.Entry<Person,String>> allSet = null ; // 準備使用Set接收全部內容
allSet = map.entrySet() ;
Iterator<Map.Entry<Person,String>> iter = null ;
iter = allSet.iterator() ;
while(iter.hasNext()){
Map.Entry<Person,String> me = iter.next() ;
System.out.println(me.getKey() + " --> " + me.getValue()) ;
}
}
};
import java.util.IdentityHashMap ;
import java.util.Set ;
import java.util.Iterator ;
import java.util.Map ;
class Person{
private String name ;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
public boolean equals(Object obj){
if(this==obj){
return true ;
}
if(!(obj instanceof Person)){
return false ;
}
Person p = (Person)obj ;
if(this.name.equals(p.name)&&this.age==p.age){
return true ;
}else{
return false ;
}
}
public int hashCode(){
return this.name.hashCode() * this.age ;
}
public String toString(){
return "姓名:" + this.name + ",年齡:" + this.age ;
}
};
public class IdentityHashMapDemo02{
public static void main(String args[]){
Map<Person,String> map = null ; // 聲明Map對象
map = new IdentityHashMap<Person,String>() ;
map.put(new Person("張三",30),"zhangsan_1") ; // 加入內容
map.put(new Person("張三",30),"zhangsan_2") ; // 加入內容
map.put(new Person("李四",31),"lisi") ; // 加入內容
Set<Map.Entry<Person,String>> allSet = null ; // 準備使用Set接收全部內容
allSet = map.entrySet() ;
Iterator<Map.Entry<Person,String>> iter = null ;
iter = allSet.iterator() ;
while(iter.hasNext()){
Map.Entry<Person,String> me = iter.next() ;
System.out.println(me.getKey() + " --> " + me.getValue()) ;
}
}
};就算是兩個對象的內容相等,但是因為都是用了new關鍵字,所以地址肯定不等,那麼就可以加入進去,key是可以重複的。SortedMap接口擴展方法

import java.util.Map ;
import java.util.SortedMap ;
import java.util.TreeMap ;
public class SortedMapDemo{
public static void main(String args[]){
SortedMap<String,String> map = null ;
map = new TreeMap<String,String>() ; // 通過子類實例化接口對象
map.put("D、jiangker","https://www.jiangker.com/") ;
map.put("A、mldn","www.mldn.cn") ;
map.put("C、zhinangtuan","www.zhinangtuan.net.cn") ;
map.put("B、mldnjava","www.mldnjava.cn") ;
System.out.print("第一個元素的內容的key:" + map.firstKey()) ;
System.out.println(":對應的值:" + map.get(map.firstKey())) ;
System.out.print("最後一個元素的內容的key:" + map.lastKey()) ;
System.out.println(":對應的值:" + map.get(map.lastKey())) ;
System.out.println("返回小於指定範圍的集合:") ;
for(Map.Entry<String,String> me:map.headMap("B、mldnjava").entrySet()){
System.out.println("\t|- " + me.getKey() + " --> " + me.getValue()) ;
}
System.out.println("返回大於指定範圍的集合:") ;
for(Map.Entry<String,String> me:map.tailMap("B、mldnjava").entrySet()){
System.out.println("\t|- " + me.getKey() + " --> " + me.getValue()) ;
}
System.out.println("部分集合:") ;
for(Map.Entry<String,String> me:map.subMap("A、mldn","C、zhinangtuan").entrySet()){
System.out.println("\t|- " + me.getKey() + " --> " + me.getValue()) ;
}
}
};
最後更新:2017-04-03 14:54:32