阅读700 返回首页    go 阿里云 go 技术社区[云栖]


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

  上一篇:go 手机卫士04-自定义显示图片
  下一篇:go 仿大总点评浮动效果