Java基础17--Map(二)

2014-11-24 03:32:05 · 作者: · 浏览: 1
是一个HashMap的实例,他的底层代码是根据HashMap实现的。

2,Hashtable

Hashtable下有一个子类是Properties,表示了一个持久的属性集,用来存储键值对型的配置文件的信息,可以和IO技术相结合使用。

17-7,HashMap存储自定义对象

1,将Person对象和Person的归属地通过键与值存储到Map集合中。

//先定义Person类
public class Person {
	private String name;
	private int age;
	Person(String name,int age) {
		this.name = name;
		this.age = age;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return this.name;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getAge() {
		return this.age;
	}
}
public class Demo {
	public static void main(String[] args) {
		HashMap
  
    hm = new HashMap
   
    (); hm.put(new Person("lisi",38),"北京"); hm.put(new Person("zhaoliu",24),"上海"); hm.put(new Person("xiaoqiang",31),"沈阳"); hm.put(new Person("wangcai",28),"大连"); hm.put(new Person("zhaoliu",24),"铁岭"); Iterator
    
      it = hm.keySet().iterator(); while(it.hasNext()) { Person key = it.next(); String value = hm.get(key); System.out.println(key.getName()+":"+key.getAge()+value); } } } 
    
   
  

直接这样打印,五个人都会被打印出来,但我们认为键相同则会覆盖,这就必须在Person类中覆写hashCode()和equals()方法,是Student对象具备比较功能,这样就可以不重复取出了。

17-8,TreeMap存储自定义对象

与TreeSet一样,TreeMap也具备自定义排序的功能,他是根据Key排序的,Key的对象要实现Comparable接口,并覆写其compareTo方法,下面示例根据Person的age排序的实现。

public class Person {
	private String name;
	private int age;
	Person(String name,int age) {
		this.name = name;
		this.age = age;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return this.name;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getAge() {
		return this.age;
	}
	public int compareTo(Object o) {
		Person p = (Person)o;
		int temp = this.age - p.age;
		return temp == 0   this.name.compareTo(p.name) : temp;
	}
}
public class Demo {
	public static void main(String[] args) {
		TreeMap
  
    tm = new TreeMap
   
    (); tm.put(new Person("lisi",38),"北京"); tm.put(new Person("zhaoliu",24),"上海"); tm.put(new Person("xiaoqiang",31),"沈阳"); tm.put(new Person("wangcai",28),"大连"); tm.put(new Person("zhaoliu",24),"铁岭"); Iterator
    
     > it = tm.entrySet().iterator(); while(it.hasNext()) { Map.Entry
     
       me = it.next(); Person key = me.getKey(); String value = me.getValue(); System.out.println(key.getName()+":"+key.getAge()+value); } } } 
     
    
   
  

Person实现了Comparable接口,其compareTo方法是按照年龄排序的,所以上述程序打印结果是按照年龄排序的。

这个TreeMap也可以通过比较器来排序,只要在创建TreeMap对象时,直接将比较器的实例对象作为实参传给其构造函数即可。

public class ComparatorsByName implements Comparator {
	public int compare(Object o1,Object o2) {
		Person p1 = (Person)o1;
		Person p2 = (Person)o2;
		int temp = p1.getName().compareTo(p2.getName());
		return temp == 0   p1.getAge() - p2.getAge() : temp;
	}
}

...

构建的时候:

TreeMap tm = newTreeMap (new ComparatorByName());

17-9,LinkedHashMap

需求:让HashMap中的元素有序的取出来,即怎么存的就怎么取出。

直接在HashMap的前面加上Linked就可以了。

如:HashMap hm =new LinkedHashMap ();

加上链表结构就可以有序存入取出了。

17-10,Map集合-练习

需求:记录字母出现的次数。

"fdgavcbsacdfs",获取该字符串中每一个字母出现的次数,

要求打印结果格式:a(2)b(1)...

思路:

对于结果的分析发现,字母和数字之间存在着映射关系。而且这种关系很多,很多数据就需要存储起来,能存储映射关系的容器有数组和Map集合。

那么还要思考,数组是基于角标的,角标是有序的编码,在本题中,映射关系的一方是有序的编码么?不是的,所以应该使用Map集合,又发现可以保证唯一性的一方具备这顺序,如a,b,c...,所以可以使用TreeMap集合。

这个集合最终应该存储的是字母和次数的对应关系。

(1)因为操作的是字符串中的字母,所以先将字符串编程字符数组。

(2)遍历字符数组,用每一个字母作为键去查Map集合这个表。如果该字母键不存在,就将该字母作为键,1作为值存入到Map集合中。如果该字母键不