java集合TreeMap使用自然排序,定制排序

import java.util.Comparator;
import java.util.TreeMap;

public class Demo3 {  
    public static void main(String[] args) 
    {  
    	System.out.println("使用自然排序:");
    	TreeMap treeMap=new TreeMap();
    	treeMap.put(new IdNumber2(1000), "王尼玛");
    	treeMap.put(new IdNumber2(1001), "万尼玛");
    	treeMap.put(new IdNumber2(1002), "蓝尼玛");
    	treeMap.put(new IdNumber2(1003), "冯尼玛");
    	treeMap.put(new IdNumber2(1004), "秦尼玛");
    	System.out.println("treeMap="+treeMap);
    	System.out.println("使用定制排序:(从小到大排序)");
    	//使用钉子排序,指明比较器
    	TreeMap treeMap2=
    			new TreeMap(new MyComparatorBigToSmall());
    	treeMap2.put(new IdNumber2(1000), "王尼玛");
    	treeMap2.put(new IdNumber2(1001), "万尼玛");
    	treeMap2.put(new IdNumber2(1002), "蓝尼玛");
    	treeMap2.put(new IdNumber2(1003), "冯尼玛");
    	treeMap2.put(new IdNumber2(1004), "秦尼玛");
    	System.out.println("treeMap2="+treeMap2);
    	System.out.println("使用定制排序:(从大到小排序)");
    	TreeMap treeMap3=
    			new TreeMap(new MyComparatorSmallToBig());
    	treeMap3.put(new IdNumber2(1000), "王尼玛");
    	treeMap3.put(new IdNumber2(1001), "万尼玛");
    	treeMap3.put(new IdNumber2(1002), "蓝尼玛");
    	treeMap3.put(new IdNumber2(1003), "冯尼玛");
    	treeMap3.put(new IdNumber2(1004), "秦尼玛");
    	System.out.println("treeMap3="+treeMap3);
    }  
}  
  //自定义比较器:实现comparator接口
class MyComparatorBigToSmall implements Comparator //使用泛型
{  
 
	@Override
	public int compare(IdNumber2 o1, IdNumber2 o2) {
		// TODO Auto-generated method stub
		return o1.getIdNumber()-o2.getIdNumber();//从小到大排序
	}
} 
class MyComparatorSmallToBig implements Comparator
{
	@Override
	public int compare(IdNumber2 o1, IdNumber2 o2) {
		// TODO Auto-generated method stub
		return o2.getIdNumber()-o1.getIdNumber();//从小到大排序
	}
}
//z作为键的类实现Comparable接口
class IdNumber2 implements Comparable
{
	//成员属性
	private int IdNumber;
	public int getIdNumber()
	{
		return IdNumber;
	}
	//构造函数
	public IdNumber2() {
		// TODO Auto-generated constructor stub
	}
	//构造函数
	public IdNumber2(int IdNumber)
	{
		this.IdNumber=IdNumber;
	}
	//重写toString()方法
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "["+this.IdNumber+"]";
	}
	//重写equals()方法
	@Override
	public boolean equals(Object obj) 
	{
		if(obj==this)//自己和自己比较
			return true;//那就是同一个相等了
		//传入的引用不为空,而且两个的类型一样
		if(obj!=null&&obj.getClass()==this.getClass())
		{
			IdNumber2 idNumber2=(IdNumber2) obj;//类型转换
			return this.IdNumber==idNumber2.IdNumber;
			//如果两个的编号一样,那就认为相等
		}
		return false;
	}
	//重写hashCode()方法
	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return this.IdNumber*100;
	}
	//重写compareTo()方法实现自然排序
	@Override
	public int compareTo(IdNumber2 o) 
	{
		// TODO Auto-generated method stub
		//return this.IdNumber-o.IdNumber;//从小到大排序:
		return o.IdNumber-this.IdNumber;//从大到小排序
	}
}

结果:
使用自然排序:
treeMap={[1004]=秦尼玛, [1003]=冯尼玛, [1002]=蓝尼玛, [1001]=万尼玛, [1000]=王尼玛}
使用定制排序:(从小到大排序)
treeMap2={[1000]=王尼玛, [1001]=万尼玛, [1002]=蓝尼玛, [1003]=冯尼玛, [1004]=秦尼玛}
使用定制排序:(从大到小排序)
treeMap3={[1004]=秦尼玛, [1003]=冯尼玛, [1002]=蓝尼玛, [1001]=万尼玛, [1000]=王尼玛}

你可能感兴趣的:(java集合TreeMap使用自然排序,定制排序)