Java中HashMap、TreeMap与Comparable、Comparator的关联使用

    我们知道,在Map接口中,HashMap类存储元素是按照元素的key的hashcode存放的,HashMap里面存入的键值对在取出的时候是随机的,它根据键的HashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度。在Map 中插入、删除和定位元素,HashMap 是最好的选择。但是如果想要使元素按照自定义的顺序排序,就要用到TreeMap了。

    TreeMap不是按照hashcode存放的,而是按照实现的comparable接口的compareTo这个方法来存储的,只要compareTo的返回结果为0就表示两个对象相等,那么就存不进去两个对象,后put的会把前面的覆盖掉。

1、通过实现comparable接口排序

先创建TestPerson类

import java.util.Objects;

public class TestPerson implements Comparable{
	private String name;
	private Integer age;
	TestPerson(String name,int age){
		this.name = name;
		this.age = age;
	}
	public String getName(){
		return this.name;
	}
	public int getAge(){
		return this.age;
	}
	@Override 
	public boolean equals(Object o){
		if(o instanceof TestPerson){
			TestPerson p = (TestPerson)o;
			return this.name.equals(p.name) && this.age==p.age;
		}
		else
			return false;
	}
    @Override  
    public int hashCode() {  
        return Objects.hash(name, age);  
    }
	@Override
	public int compareTo(Object o) {
		// TODO Auto-generated method stub
		TestPerson tp = (TestPerson)o;
		return this.age > tp.getAge() ? 1 : this.age == tp.getAge() ? 0 : -1;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return name + "(" + age.toString() + ")";
	}
} 

测试用例:

import java.util.Map;
import java.util.TreeMap;
 
public class TreeMapTest {
	public static void main(String[] args) {
		Map<TestPerson,String> m = new TreeMap<TestPerson,String>();
		m.put(new TestPerson("Jack Ma",51),"Alibaba");
		m.put(new TestPerson("Jackey Cheng",60), "YaolaiYingCheng");
		m.put(new TestPerson("Jay Chou",35),"YeHuimei");
		m.put(new TestPerson("Ma Huateng",53),"Tencent");
		System.out.println(m);
	}	
}

输出:

{Jay Chou(35)=YeHuimei, Jack Ma(51)=Alibaba, Ma Huateng(53)=Tencent, Jackey Cheng(60)=YaolaiYingCheng}

2、通过实现comparator接口实现排序

前面的例子是按年龄排序,本例按姓名排序

import java.util.Comparator;
import java.util.Objects;

public class TestPerson implements Comparator<TestPerson>{
	private String name;
	private Integer age;
	public TestPerson(String name,int age){
		this.name = name;
		this.age = age;
	}
	public TestPerson(){
		
	}
	public String getName(){
		return this.name;
	}
	public int getAge(){
		return this.age;
	}
	@Override 
	public boolean equals(Object o){
		if(o instanceof TestPerson){
			TestPerson p = (TestPerson)o;
			return this.name.equals(p.name) && this.age==p.age;
		}
		else
			return false;
	}
    @Override  
    public int hashCode() {  
        return Objects.hash(name, age);  
    }
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return name + "(" + age.toString() + ")";
	}
	@Override
	public int compare(TestPerson p1, TestPerson p2) {
		// TODO Auto-generated method stub
		//return p1.getAge()>p2.getAge()? 1 : p1.getAge() == p2.getAge()? 0:-1;
		return p1.getName().compareTo(p2.getName());
	}
}

测试用例

import java.util.Map;
import java.util.TreeMap;
 
public class TreeMapTest {
	public static void main(String[] args) {
		Map<TestPerson,String> m = new TreeMap<TestPerson,String>(new TestPerson());
		m.put(new TestPerson("Jack Ma",51),"Alibaba");
		m.put(new TestPerson("Jackey Cheng",60), "YaolaiYingCheng");
		m.put(new TestPerson("Jay Chou",35),"YeHuimei");
		m.put(new TestPerson("Ma Huateng",53),"Tencent");
		System.out.println(m);
	}	
}

输出

{Jack Ma(51)=Alibaba, Jackey Cheng(60)=YaolaiYingCheng, Jay Chou(35)=YeHuimei, Ma Huateng(53)=Tencent}

猜你喜欢

转载自blog.csdn.net/u010183728/article/details/80056143