Java 容器(四) Map

纸上得来终觉浅

1.Map的使用估计是最常见的了: 

1)HashMap:存储的值可以重复,键不能;不能使用Comparator和Comparable接口排序;HashMap最多只允许一条记录的键为null,不允许多条记录的值为null

2)TreeMap:存储的值可以重复,键不能;自动排序或手动排序;

3)LinkedHashMap:存储的值可以重复,键不能;保持输入的顺序

其中它们的用法和Set对比,几乎是一一对应:

package roadArchitectWeb.Test;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
class Student{
	private Integer id;
	private String name;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Student(Integer id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + "]";
	}
	@Override
	public int hashCode() {
		return this.id;
	}
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Student){
			Student stu = (Student)obj;
			if(stu.name == this.name && stu.id == this.id)
				return true;
		}
		return false;
	}
}
public class Test5 {
		protected static Map<String,Student> hashMap = new HashMap<String,Student>();
		/*Comparable的排序会失效,因为map只能按键排列*/
		protected static Map<String,Student> treeMapComparable = new TreeMap<String,Student>();
		protected static Map<String,Student> treeMapComparator = new TreeMap<String,Student>(new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				return o2.compareTo(o1);
			}
		});
		protected static Map<String,Student> linkedHashMap = new LinkedHashMap<String,Student>();
		public static void main(String[] args) {
		hashMap.put("1",new Student(3,"sun"));
		hashMap.put("2",new Student(1,"zhao"));
		hashMap.put("3",new Student(2,"qian"));
		System.out.println("hashMap:"+hashMap);
		/*
		 *这里使用Comparable
		 */
		treeMapComparable.put("3",new Student(3,"sun"));
		treeMapComparable.put("2",new Student(1,"zhao"));
		treeMapComparable.put("1",new Student(2,"qian"));
		System.out.println("treeMapComparable:"+treeMapComparable);
		
		/*这里使用Comparator
		 */
		treeMapComparator.put("3",new Student(3,"sun"));
		treeMapComparator.put("1",new Student(1,"zhao"));
		treeMapComparator.put("2",new Student(2,"qian"));
		System.out.println("treeMapComparator:"+treeMapComparator);
		
		linkedHashMap.put("3",new Student(3,"sun"));
		linkedHashMap.put("1",new Student(1,"zhao"));
		linkedHashMap.put("2",new Student(2,"qian"));
		System.out.println("linkedHashMap:"+linkedHashMap);
	}
}
结果如下:

hashMap:{1=Student [id=3, name=sun], 2=Student [id=1, name=zhao], 3=Student [id=2, name=qian]}
treeMapComparable:{1=Student [id=2, name=qian], 2=Student [id=1, name=zhao], 3=Student [id=3, name=sun]}
treeMapComparator:{3=Student [id=3, name=sun], 2=Student [id=2, name=qian], 1=Student [id=1, name=zhao]}
linkedHashMap:{3=Student [id=3, name=sun], 1=Student [id=1, name=zhao], 2=Student [id=2, name=qian]}

2.注意

1)同Set,hashMap和LinkedHashMap不支持排序;

2)hashTable:基本上与hashMap相同,它不允许记录的键或者值为空;线程同步;

3)HashSet是通过HashMap实现的,TreeSet是通过TreeMap实现的,只不过Set用的只是Map的key;

4)另外注意到,treeMap当key是基础类,如String时候,似乎只能使用Comparator接口,基础类的Comparable接口是内置的(TreeMap排序使用Comparable没有实现过)

3.Map中迭代器使用:

/*遍历key*/
		Iterator it1 = hashMap.keySet().iterator();
		while(it1.hasNext()){
			System.out.println("key:"+it1.next());
		}
		/*遍历entry,从而可以获得key或者value的值*/
		Iterator it2 = hashMap.entrySet().iterator();
		while(it2.hasNext()){
			Map.Entry<String, Student> entry = null;
			entry = (Entry<String, Student>) it2.next();
			System.out.println("value:"+entry.getValue());
		}
结果如下:

key:1
key:2
key:3
value:Student [id=3, name=sun]
value:Student [id=1, name=zhao]
value:Student [id=2, name=qian]

你可能感兴趣的:(map,HashMap,LinkedHashMap,Hashtable,TreeMap)