java SE基础(Comparable接口和Comparator接口)

1。Comparable(可比较接口)

(1)此接口强行对实现它的每个类的对象进行整体排序。这种排序被称为类的自然排序,类的 compareTo 方法被称为它的自然比较方法

(2)实现此接口的对象可以通过Collections.sort和Arrays.sort进行自动排序,也可以用作有序映射中的键或有序集合中的元素,无需指定比较器

(3)接口声明*/
                public interface Comparable<T>{
                    int compareTo(T o);    //比较此对象与指定对象的顺序。如果该对象小于、等于或大于指定对象,则分别返回负整数、零或正整数
                }

(4)例如:

public class Person implements Comparable<Person>{
	private int num;
	private int age;
	
	public Person(int num , int age) {
		this.num = num;
		this.age = age;
	}

	@Override
	public int compareTo(Person o) {
		return this.num - o.num;
	}
	
	@Override
	public String toString() {
		return this.num+"--"+this.age;
	}
}

import java.util.Iterator;
import java.util.TreeSet;
import java.util.Random;

public class MyTreeSet {
	public static void main(String[] args) {
		TreeSet<Person> treeset = new TreeSet<Person>();
		
		Random rnum = new Random();
		Random rage = new Random();
		int count = 0 , age , num;
		while(count < 10){
			num = rnum.nextInt(30);
			age  = rage.nextInt(30);
			Person person = new Person(num , age);
			if(!treeset.contains( person )){
				treeset.add(person);
				count++;
			}
		}
		
		Iterator<Person> iterator = treeset.iterator();
		while(iterator.hasNext())
			System.out.print(iterator.next().toString()+"\t\t");
		System.out.println();
	}
}

2。Comparator(比较器接口)

(1)可以将Comparator传递给sort方法,从而允许在排序顺序上实现精确控制。还可为那些没有自然顺序的对象 collection 提供排序

(2)接口声明*/
                public interface Comparator<T>{
                    int compare(T o1, T o2);        //比较用来排序的两个参数。根据第一个参数小于、等于或大于第二个参数分别返回负整数、零或正整数
                }

(3)例如:

import java.util.Comparator;

public class Person implements Comparator<Person>{
	private int num;
	private int age;
	
	public Person(int num , int age) {
		this.num = num;
		this.age = age;
	}
	public Person() {
		super();
	}
	public int getAge() {
		return age;
	}
	public int getNum() {
		return num;
	}
	@Override
	public String toString() {
		return this.num+"--"+this.age;
	}
	@Override
	public int compare(Person arg0, Person arg1) {
		return arg0.getAge() - arg1.getAge();
	}
}

import java.util.Iterator;
import java.util.Random;
import java.util.TreeSet;

public class MyTreeSet{
	public static void main(String[] args) {
		TreeSet<Person> treeset = new TreeSet<Person>(new Person());
		
		Random rnum = new Random();
		Random rage = new Random();
		int count = 0 , age , num;
		while(count < 10){
			num = rnum.nextInt(30);
			age  = rage.nextInt(30);
			Person person = new Person(num , age);
			if(!treeset.contains( person )){
				treeset.add(person);
				count++;
			}
		}
		
		Iterator<Person> iterator = treeset.iterator();
		while(iterator.hasNext())
			System.out.print(iterator.next().toString()+"\t\t");
		System.out.println();
	}
}
例二

public class Person{
	private int num;
	private int age;
	
	public Person(int num , int age) {
		this.num = num;
		this.age = age;
	}
	public int getAge() {
		return age;
	}
	public int getNum() {
		return num;
	}
	@Override
	public String toString() {
		return this.num+"--"+this.age;
	}
}

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
import java.util.Random;

public class MyTreeSet implements Comparator<Person>{
	public static void main(String[] args) {
		TreeSet<Person> treeset = new TreeSet<Person>(new MyTreeSet());
		
		Random rnum = new Random();
		Random rage = new Random();
		int count = 0 , age , num;
		while(count < 10){
			num = rnum.nextInt(30);
			age  = rage.nextInt(30);
			Person person = new Person(num , age);
			if(!treeset.contains( person )){
				treeset.add(person);
				count++;
			}
		}
		
		Iterator<Person> iterator = treeset.iterator();
		while(iterator.hasNext())
			System.out.print(iterator.next().toString()+"\t\t");
		System.out.println();
	}

	@Override
	public int compare(Person arg0, Person arg1) {
		return arg0.getAge() - arg1.getAge();
	}
}


你可能感兴趣的:(java,comparable,comparator)