java Map - HashMap, TreeMap 연습

/*
复习:
只要底层数据结构是哈希表的集合,都要覆盖重写两个方法来保证哈希表中元素的唯一性。
public int hashCode()
public boolean equals(Object obj)

(1)HashMap能够保证元素的唯一性,不重复;
(原理:在对象类中覆盖hashCode()和equals()方法),
对象类可以实现Comparable接口,并实现compareTo()方法,这样对象就觉有比较性了,在HashMap集合中就具有顺序。
(2)TreeMap能够使得元素按照一定的顺序存储,这个排序规则可默认(自然顺序),可以自定义比较器。
TreeMap在存储自定义对象时,两种方法保证存储的顺序性。
1,对象自身具有比较性;即存储的对象类实现Comparable接口,并实现compareTo()方法
2,自定义比较器,即class MyCompare implements Comparator{//实现compare(Object obj1,Object obj2)方法}
并将比较器对象作为参数传递到TreeMap构造方法中。


练习:
每个学生都有对应归属地。
学生Student,地址String
学生属性:姓名,年龄
注意:姓名和年龄相同的学生视为同一学生。
保证学生的唯一性。

1,描述学生
2,定义map容器,将学生作为键,地址作为值。存入。
3,获取map容器的值。

*/
import java.util.*;
class Student implements Comparable<Student>
{
	private String name;
	private int age;
	Student(String name,int age)
	{
		this.name=name;
		this.age=age;
	}

	public String getName()
	{
		return name;
	}

	public int getAge()
	{
		return age;
	}
/*   如果没有指定Comparable泛型为Student,那么参数需要为Object obj
	public int compareTo(Object obj)   //按照学生年龄进行排序
	{
		if(!(obj instanceof Student))
			throw new RuntimeException("不是学生");
		Student s=(Student)obj;
		int num=new Integer(this.age).compareTo(new Integer(s.age));
		if(num==0)
			return this.name.compareTo(s.name);
		return num;
	}
*/
	public int compareTo(Student s)   //按照学生年龄进行排序
	{
		int num=new Integer(this.age).compareTo(new Integer(s.age));
		if(num==0)
			return this.name.compareTo(s.name);
		return num;
	}
		


	public int hashCode()
	{
		return name.hashCode()+age*34;
	}

	public boolean equals(Object obj)
	{
		if(!(obj instanceof Student))
			throw new RuntimeException("不是学生");
		Student s=(Student)obj;
		if(this.name.equals(s.name) && this.age==s.age)
			return true;
		return false;
	}
}

class MapTest 
{
	public static void main(String[] args) 
	{
		HashMap<Student,String> map=new HashMap<Student,String>();

		map.put(new Student("zhangsan002",20),"place001");
		map.put(new Student("zhangsan003",30),"place003");
		map.put(new Student("zhangsan005",40),"place005");
		map.put(new Student("zhangsan004",50),"place004");
		map.put(new Student("zhangsan002",20),"place009");
		map.put(new Student("zhangsan001",70),"place001");


		//方式一
		Set<Student> set=map.keySet();
		Iterator<Student> it=set.iterator();
		while(it.hasNext())
		{
			Student s=it.next();
			String place=map.get(s);
			sop("name:"+s.getName()+"..age:"+s.getAge()+".."+place);
		}

		//方式二
		Set<Map.Entry<Student,String>> entryset=map.entrySet();
		Iterator<Map.Entry<Student,String>> it1=entryset.iterator();
		while(it1.hasNext())
		{
			Map.Entry<Student,String> me=it1.next();
			Student s1= me.getKey();
			String place1=me.getValue();
			sop("name:"+s1.getName()+".........age:"+s1.getAge()+"..........place:"+place1);
		}
	}

	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}

위 에 Comparable 인 터 페 이 스 를 사용 하여 학생 대상 을 비교 시 켰 다 (학생 연령 에 따라 순 위 를 매 긴 다).
다음은 다른 비교 방법 인 사용자 정의 비교 기 를 사용 하여 Comparator 인 터 페 이 스 를 실현 하고 학생 이름 에 따라 정렬 합 니 다.
/*
按照学生姓名排序存储学生对象。此时要定义比较器来实现自定义排序方法。
*/
import java.util.*;
class MapTest2 
{
	public static void main(String[] args) 
	{
		TreeMap<Student,String> tm=new TreeMap<Student,String>(new MyComp());
		tm.put(new Student("zhangsan002",20),"place001");
		tm.put(new Student("zhangsan003",30),"place003");
		tm.put(new Student("zhangsan005",40),"place005");
		tm.put(new Student("zhangsan004",50),"place004");
		tm.put(new Student("zhangsan002",20),"place009");
		tm.put(new Student("zhangsan001",70),"place001");
		tm.put(new Student("zhangsan001",30),"place001");

		Set<Student> set=tm.keySet();
		Iterator<Student> it=set.iterator();
		while(it.hasNext())
		{
			Student s=it.next();
			String place=tm.get(s);
			MapTest.sop("name:"+s.getName()+"..age:"+s.getAge()+".."+place);
		}
	}
}

class MyComp implements Comparator<Student>  //按照姓名排序。
{
	public int compare(Student s1,Student s2)
	{
		int num=s1.getName().compareTo(s2.getName());
		if(num==0)
		{
			return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
		}
		return num;
	}
}

좋은 웹페이지 즐겨찾기