java_Comparable & Comparator_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java_Comparable & Comparator

java_Comparable & Comparator

 2011/9/22 8:13:24  543089122  http://jqsl2012.iteye.com  我要评论(0)
  • 摘要:java有2个非常重要的排序接口:java.lang.Comparable和java.util.Comparator,前者是基础包下的,主要通过继承该接口实现类的排序功能,工具包下的主要通过非继承的方式实现排序。packagesunfa;importjava.util.Arrays;importjava.util.Comparator;publicclassComparableDemo1{publicstaticvoidmain(String[]args){/***一个类实现了java
  • 标签:Java
java有2个非常重要的排序接口:java.lang.Comparable和java.util.Comparator,
前者是基础包下的,主要通过继承该接口实现类的排序功能,工具包下的主要通过非继承的方式实现排序。

package sunfa;

import java.util.Arrays;
import java.util.Comparator;

public class ComparableDemo1 {
	public static void main(String[] args) {
		/**
		 * 一个类实现了java.lang.Comparable接口可以实现排序。
		 * 或者一个类利用java.util.Comparator接口实现排序
		 */
		Person per[] = { new Person("张三", 10, 75), new Person("李四", 11, 85),
				new Person("王五", 9, 75), new Person("陈留", 13, 75) };
		//先按souce由小到大,再由age由大到小的顺序排列
		 Arrays.sort(per);
//		Arrays.sort(per, new Comparator<Person>() {
//			public int compare(Person o1, Person o2) {
//				if (o1.getSouce() > o2.getSouce()) {
//					return 1;
//				} else if (o1.getSouce() < o2.getSouce()) {
//					return -1;
//				} else {
//					if (o1.getAge() > o2.getAge()) {
//						return -1;
//					} else if (o1.getAge() < o2.getAge()) {
//						return 1;
//					} else {
//						return 0;
//					}
//				}
//			}
//		});
		System.out.println("name\tage\tsouce");
		for (int i = 0; i < per.length; i++) {
			System.out.println(per[i]);
		}
	}
}

class Person implements Comparable<Person>{
	private String name;
	private int age;
	private float souce;

	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public float getSouce() {
		return souce;
	}

	public void setSouce(float souce) {
		this.souce = souce;
	}

	public Person(String name, int age, float souce) {
		super();
		this.name = name;
		this.age = age;
		this.souce = souce;
	}

	public String toString() {
		return this.name + "\t" + this.age + "\t" + this.souce;
	}

	public int compareTo(Person o) {
		if (this.souce > o.souce) {
			return 1;
		} else if (this.souce < o.souce) {
			return -1;
		} else {
			if (this.age > o.age) {
				return -1;
			} else if (this.age < o.age) {
				return 1;
			} else {
				return 0;
			}
		}
	}
}



java.util.TreeMap是红黑树的实现版本,并且支持自定义排序
public TreeMap(Comparator<? super K> c) {
        this.comparator = c;
    }



java.util.Arrays.stors(...)和TreeMap分别是利用二分法和平衡hashu.html" target="_blank">二叉树来实现对象排序功能的。

package sunfa;

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

class Person1 implements Comparator<Person1> {
	private String name;
	private int age;
	private float souce;

	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public float getSouce() {
		return souce;
	}

	public void setSouce(float souce) {
		this.souce = souce;
	}

	public Person1() {
		super();
	}

	public Person1(String name, int age, float souce) {
		super();
		this.name = name;
		this.age = age;
		this.souce = souce;
	}

	public String toString() {
		return this.name + "\t" + this.age + "\t" + this.souce;
	}

	public int compareTo(Person1 o) {
		return 0;
	}

	public int compare(Person1 o1, Person1 o2) {
		if (o1.souce > o2.souce) {
			return 1;
		} else if (o1.souce < o2.souce) {
			return -1;
		} else {
			if (o1.age > o2.age) {
				return -1;
			} else if (o1.age < o2.age) {
				return 1;
			} else {
				return 0;
			}
		}
	}
}
public class TreeMapTest {
	public static void main(String[] args) {
		TreeMap<Person1, Person1> tree = new TreeMap<Person1, Person1>(
				new Person1());
		Person1 per[] = { new Person1("张三", 10, 75), new Person1("李四", 11, 85),
				new Person1("王五", 9, 75), new Person1("陈留", 13, 75) };
		for (int i = 0; i < per.length; i++) {
			tree.put(per[i], null);
		}
		Iterator<Person1> it = tree.keySet().iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}
	}
}
发表评论
用户名: 匿名