集合之Map接口3--TreeMap

TreeMap概述

一、TreeMap存储 Key-Value 对时,需要根据 key-value 对进行排序。TreeMap 可以保证所有的 Key-Value 对处于有序状态。
二、TreeMap 的 Key 的排序(与TreeSet类似,只是需要将键的集取出来再进行排序):
1、自然排序:TreeMap 的所有的 Key 必须实现 Comparable 接口,而且所有的 Key 应该是同一个类的对象,否则将会抛出 ClasssCastException
2、定制排序:创建 TreeMap 时,传入一个 Comparator 对象,该对象负责对 TreeMap 中的所有 key 进行排序。此时不需要 三、Map 的 Key 实现 Comparable 接口
TreeMap判断两个key相等的标准:两个key通过compareTo()方法或者compare()方法返回0。
四、若使用自定义类作为TreeMap的key,所属类需要重写equals()和hashCode()方法,且equals()方法返回true时,compareTo()方法应返回0。
自然排序实现代码:

	@Test // 自然排序
	public void test3() {
		Map map = new TreeMap();
		map.put(new Person(12, "samureo"), 11);
		map.put(new Person(2, "zhoaxueli"), 12);
		map.put(new Person(23, "taotao"), 113);
		map.put(new Person(22, "wenwen"), 1124);
		map.put(new Person(22, "yingeno"), 114);
		Set set = map.keySet();
		for (Object o : set) {
			System.out.println(o + " " + map.get(o));
		}
	}

插入对象实现代码

public class Person implements Comparable{
	private Integer age;
	protected String name;
	int id=1001;
	public Integer getAge() {
		return age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Person() {// 默认定义的,一旦显示定义构造器,显示的构造器不在提供,构造器重载
		age = 18;
	}
	public Person(int age,String name) {
		this.age = age;
		this.name=name;
	}
	@Override
	public String toString() {
		return "Person [age=" + age + ", name=" + name + ", id=" + id + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + id;
		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;
		Person other = (Person) obj;
		if (age != other.age)
			return false;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	@Override
	public int compareTo(Object o) {
		// TODO Auto-generated method stub
		if (o instanceof Person) {
			Person person=(Person)o;
			/*return this.name.compareTo(person.name);*/
			/*return this.age.compareTo(person.age);*///加个负号,从大到小
			int i=this.age.compareTo(person.age);
			if (i==0) {
				return this.name.compareTo(person.name);
			}else {
				return i;
			}
		}
		return 0;
	}
}

定制排序

// 定制排序
	@Test
	public void test4() {
		Comparator comparator = new Comparator() {

			@Override
			public int compare(Object o1, Object o2) {
				// TODO Auto-generated method stub
				if (o1 instanceof Person && o2 instanceof Person) {
					Person person1 = (Person) o1;
					Person person2 = (Person) o2;
					int i = person1.name.compareTo(person2.name);
					if (i == 0) {
						return person1.getAge().compareTo(person2.getAge());
					} else {
						return i;
					}
				}
				return 0;
			}
		};
		TreeMap map = new TreeMap(comparator);
		map.put(new Person(12, "samureo"), 11);
		map.put(new Person(2, "zhoaxueli"), 12);
		map.put(new Person(23, "taotao"), 113);
		map.put(new Person(22, "wenwen"), 1124);
		map.put(new Person(22, "yingeno"), 114);
		Set set = map.keySet();
		for (Object o : set) {
			System.out.println(o + " " + map.get(o));
		}

	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值