码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA学习第三十九课(常用对象API)- 集合框架(七)— Map集合及其子类对象

时间:2014-10-20 23:27:17      阅读:346      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   使用   java   strong   sp   

一、Map集合常见子类


HashTable:内部结构是哈希表,同步,此实现提供所有可选的映射操作,不允许使用 null 值和 null 键

(HashTable下有子类Properties,使用频率非常高,用来存储键值对型的配置文件信息和IO技术相结合)

HashMap:内部结构是哈希表,不同步,此实现提供所有可选的映射操作,并允许使用 null 值和 null 键。

TreeMap:内部结构是二叉树,不同步,可以对Map集合中的键进行排序。


二、HashMap演示

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
class Man
{
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Man(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public Man() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Man other = (Man) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
}
public class Main 
{
	public static void main(String[] args) {
		HashMapDemo();
	}
	public static void HashMapDemo(){
		//人和籍贯 通过键值对存储在HashMap中
		HashMap<Man, String> hm = new HashMap<Man,String>();
		hm.put(new Man("周",1),"上海");
		hm.put(new Man("周",1),"东北");//要保证哈希表,所以Man中的HashCode和equals方法要复写
		hm.put(new Man("王",4),"北京");
		hm.put(new Man("孙",2),"广州");
		hm.put(new Man("赵",6),"山东");
		
		Iterator<Man> it =  hm.keySet().iterator();
		while(it.hasNext()){
			Man man = it.next();
			String bir = hm.get(man);
			System.out.println("man - bir :"+man.getName()+","+man.getAge()+" - "+bir);
		}	
	}
}

三、TreeMap演示

import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
class Man extends Object
{
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Man(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public Man() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Man other = (Man) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
}
class Compararule implements Comparator<Object>
{
	 public int compare(Object arg0, Object arg1) {  
		 if(!(arg0 instanceof Man))  
	            throw new ClassCastException();  
	   
	            Man man = (Man)arg0;  
	            Man man2 = (Man)arg1;
	        int te = man.getAge() - man2.getAge(); 
	        return te==0?man.getName().compareTo(man2.getName()):te;  
	    }   
}
public class Main 
{
	public static void main(String[] args) {
		TreeMapDemo();
	}
	public static void TreeMapDemo(){
		TreeMap<Man, String> tm = new TreeMap<Man,String>(new Compararule());
		tm.put(new Man("zhou",1),"上海");
		tm.put(new Man("zhou",1),"东北");//要保证哈希表,所以Man中的HashCode和equals方法要复写
		tm.put(new Man("wang",4),"北京");
		tm.put(new Man("sun",2),"广州");
		tm.put(new Man("zhao",6),"山东");
		
		Iterator<Map.Entry<Man,String>> it =  tm.entrySet().iterator();
		while(it.hasNext()){
			Map.Entry<Man, String> mEntry = it.next();
			Man man = mEntry.getKey();
			String bir = mEntry.getValue();
			System.out.println("man - bir :"+man.getName()+","+man.getAge()+" - "+bir);
		}	
	}
}


集合框架的三大集合:List、Set、Map,到此就结束了
看到array,数组,有角标,查询速度快
看到link,链表,增删速度快,add、get、remove frist/last 方法
看到hash,哈希表,元素唯一性,覆盖HashCode方法和equals方法
看到tree,二叉树,排序,两大接口Comparator,Comparable

而Map自身没有迭代器,但是可以通过keySet,entry返回Set映射,再通过Set的迭代器,访问键值对

明天就是泛型的学习。要重点掌握,加油!!!

JAVA学习第三十九课(常用对象API)- 集合框架(七)— Map集合及其子类对象

标签:style   blog   color   io   ar   使用   java   strong   sp   

原文地址:http://blog.csdn.net/wjw0130/article/details/40318509

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!