TreeMap简单simple

TreeMap能够按照主键对里面的数据进行排序,基于上篇文章:java集合类之TreeMap中有关于TreeMap内部实现的详细介绍。本文主要是写了些使用TreeMap的简单demo。

要想实现TreeMap的自动排序功能,要么主键对象实现Comparator接口,要么用Comparable来构造TreeMap。以下则分别对这两种方式创建TreeMap。

1. 继承Comparable

public class Person implements Comparable<Person>{

	private String name;
	
	private String sex;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((sex == null) ? 0 : sex.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;
		Person other = (Person) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (sex == null) {
			if (other.sex != null)
				return false;
		} else if (!sex.equals(other.sex))
			return false;
		return true;
	}

	public Person(String name, String sex) {
		super();
		this.name = name;
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", sex=" + sex + "]";
	}

	@Override
	public int compareTo(Person o) {
		Person s = (Person) o;
		if(this.name.compareTo(s.getName())>0){
			return 1;
		}else if(this.name.compareTo(s.getName())<0){
			return -1;
		}else {
			if(this.sex.compareTo(s.getSex())>0){
				return 1;
			}else if(this.sex.compareTo(s.getSex()) < 0){
				return -1;
			}else{
				return 0;
			}
		}
	}
}

2. 实现Comparator接口

public class MyCompartor implements Comparator<Person>{

	@Override
	public int compare(Person o1, Person o2) {
		if(null == o1){
			System.out.println("o1 is null");
			return 1;
		}
		if(null == o2){
			System.out.println("o2 is null");
			return -1;
		}
		if(o1.getName().compareTo(o2.getName())>0){
			return 1;
		}else if(o1.getName().compareTo(o2.getName())<0){
			return -1;
		}else {
			if(o1.getSex().compareTo(o2.getSex())>0){
				return 1;
			}else if(o1.getSex().compareTo(o2.getSex()) < 0){
				return -1;
			}else{
				return 0;
			}
		}
	}
}

一般主键类尽量使用同一类型,如果使用不同类型则需要分别比较。

public class Student implements Comparable<Object>{
	
	private String name;
	
	private String sex;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((sex == null) ? 0 : sex.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;
		Student other = (Student) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (sex == null) {
			if (other.sex != null)
				return false;
		} else if (!sex.equals(other.sex))
			return false;
		return true;
	}

	public Student(String name, String sex) {
		super();
		this.name = name;
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", sex=" + sex + "]";
	}


	@Override
	public int compareTo(Object o) {
		if(o instanceof Student){
			Student s = (Student) o;
			if(this.name.compareTo(s.getName())>0){
				return 1;
			}else if(this.name.compareTo(s.getName())<0){
				return -1;
			}else {
				if(this.sex.compareTo(s.getSex())>0){
					return 1;
				}else if(this.sex.compareTo(s.getSex()) < 0){
					return -1;
				}else{
					return 0;
				}
			}
		}else if(o instanceof Person){
			Person s = (Person) o;
			if(this.name.compareTo(s.getName())>0){
				return 1;
			}else if(this.name.compareTo(s.getName())<0){
				return -1;
			}else {
				if(this.sex.compareTo(s.getSex())>0){
					return 1;
				}else if(this.sex.compareTo(s.getSex()) < 0){
					return -1;
				}else{
					return 0;
				}
			}
		}else {
			return 0;
		}
	}
}

public class Person implements Comparable<Object>{

	private String name;
	
	private String sex;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((sex == null) ? 0 : sex.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;
		Person other = (Person) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (sex == null) {
			if (other.sex != null)
				return false;
		} else if (!sex.equals(other.sex))
			return false;
		return true;
	}

	public Person(String name, String sex) {
		super();
		this.name = name;
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", sex=" + sex + "]";
	}

	@Override
	public int compareTo(Object o) {
		if(o instanceof Student){
			Student s = (Student) o;
			if(this.name.compareTo(s.getName())>0){
				return 1;
			}else if(this.name.compareTo(s.getName())<0){
				return -1;
			}else {
				if(this.sex.compareTo(s.getSex())>0){
					return 1;
				}else if(this.sex.compareTo(s.getSex()) < 0){
					return -1;
				}else{
					return 0;
				}
			}
		}else if(o instanceof Person){
			Person s = (Person) o;
			if(this.name.compareTo(s.getName())>0){
				return 1;
			}else if(this.name.compareTo(s.getName())<0){
				return -1;
			}else {
				if(this.sex.compareTo(s.getSex())>0){
					return 1;
				}else if(this.sex.compareTo(s.getSex()) < 0){
					return -1;
				}else{
					return 0;
				}
			}
		}else {
			return 0;
		}
	}
}

测试:

public class TreeMapExample {

	@Test
	public void test1(){
		TreeMap<Object, Object> treeMap = new TreeMap<Object, Object>();//主键不同类型
		Student s1 = new Student("a", "m");
		Student s2 = new Student("a", "c");
		Person p1 = new Person("d", "w");
		Person p2 = new Person("b", "m");
		treeMap.put(s1, s1);
		treeMap.put(s2, s2);
		treeMap.put(p1, p1);
		treeMap.put(p2, p2);
		for(Map.Entry<Object, Object> entry : treeMap.entrySet()){
			System.out.println(entry.getKey().toString());
		}
	}
	
	@Test
	public void test2(){
		MyCompartor compartor = new MyCompartor();
		TreeMap<Person, Person> treeMap = new TreeMap<Person, Person>(compartor);//主键中放入null
		Person s1 = new Person("a", "m");
		Person s2 = new Person("a", "c");
		Person p1 = new Person("d", "w");
		Person p2 = new Person("b", "m");
		Person p3 = new Person("b", "c");
		treeMap.put(s1, s1);
		treeMap.put(s2, s2);
		treeMap.put(p1, p1);
		treeMap.put(p2, p2);
		treeMap.put(null, p3);
		System.out.println(treeMap.size());
		for(Map.Entry<Person, Person> entry : treeMap.entrySet()){
			System.out.println(entry.getKey()+":"+entry.getValue());
		}
		
	}
}



posted @ 2014-10-15 19:29  marco_tan  阅读(201)  评论(0编辑  收藏  举报