Comparable与Comparator的区别

Comparable与Comparator的区别

两者都是接口,区别有哪些呢?

1.位置

Comparable位于 java.lang 下,Comparator位于 java.util 包下。

2.方法个数

Comparable接口内方法只有一个。

package java.lang;
import java.util.*;
public interface Comparable<T> {
    public int compareTo(T o);
}

Comparator接口内方法挺多,具体看API文档。

3.比较方式

实现了Comparable接口的类是可以进行内部自身比较的。可以认为是集合内比较器。

Comparator可看作是一个自定义比较器,是在集合外进行对象比较的。

4.实现方式

自定义类继承Comparable接口,在类中重写 compareTo() 方法。

自定义类继承Comparator接口,在类中重写 compare() 方法。

5.举例

Comparable举例

package demo1;

import java.util.TreeSet;
//Comparable实例
public class Demo9 {
	public static void main(String[] args) {
		TreeSet<Person> data = new TreeSet<Person>();
		Person p1 = new Person("阿三", 25);
		Person p2 = new Person("李四", 22);
		Person p3 = new Person("王五", 27);
		data.add(p1);
		data.add(p2);
		data.add(p3);
		for (Person person : data) {
			System.out.println(person);
		}	
	}
	static class Person implements Comparable<Person>{
		private String name;
		private int age;
		@Override
		public int compareTo(Person p) {
			// this 与 o 比较
			//返回的数据是: 负数-this小/零-一样大/正数-this大
			if(this.age > p.age) {
				return 1;
			}else if(this.age == p.age) {
				return 0;
			}
			return -1;
		}	
		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 Person(String name, int age) {
			this.name = name;
			this.age = age;
		}
		public Person() {
			super();
		}
		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + age;
			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 (name == null) {
				if (other.name != null)
					return false;
			} else if (!name.equals(other.name))
				return false;
			return true;
		}
		@Override
		public String toString() {
			return "Person [name=" + name + ", age=" + age + "]";
		}	
	}
}

结果为:

Person [name=李四, age=22]
Person [name=阿三, age=25]
Person [name=王五, age=27]

Comparator举例

package demo1;
import java.util.ArrayList;
import java.util.List;
public class Demo13 {
	//Comparator实例
	public static void main(String[] args) {
		List<People> data = new ArrayList<People>();
		People p1 = new People("阿三", 25);
		People p2 = new People("李四", 22);
		data.add(p1);
		data.add(p2);
		personComparator pc = new personComparator();
		Collections.sort(data, pc);
		for (int i = 0; i < data.size(); i++) {
			System.out.println(data.get(i));
		}
		System.out.println(pc.compare(p1, p2));
	}
	static class People{
		private String name;
		private int age;
		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 People(String name, int age) {
			super();
			this.name = name;
			this.age = age;
		}
		@Override
		public String toString() {
			return "People [name=" + name + ", age=" + age + "]";
		}
	}
}
package demo1;
import java.util.Comparator;
import demo1.Demo13.People;
public  class personComparator implements Comparator<People>{	
	@Override
	public int compare(People p1, People p2) {
		if(p1.getAge() > p2.getAge()) {
			return 1;
		}else if(p1.getAge() == p2.getAge()) {
			return 0;
		}
		return -1;
	}
}

结果为:

People [name=小六, age=20]
People [name=李四, age=22]
People [name=阿三, age=25]
People [name=王五, age=27]
1
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读