Comparable接口与Comparator接口的比较————Comparator接口详解

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Comparable接口与Comparator接口的比较————Comparator接口详解相关的知识,希望对你有一定的参考价值。

Comparator接口位于:java.util包中。

Comparator接口:
1. 强行对某个对象的Collection进行整体排序。
值得注意的是:
Comparator接口可以作为参数传到一些sort()方法中,例如:
Collection.sort(List<T> list, Comparator<? super T> c)、
Arrays.sort(T[] a, Comparator<? super T> c);

2. Comparator接口指定的顺序,可以不是字典顺序排序,但包含字典顺序排序!!!

现在让我们仍然对User进行排序吧!!!
Let‘s do it!!!

3. 有的人会觉得 实现Comparator接口 有点奇怪,Comparator接口中明明有两个方法: compare()、equals()
为什么实现此接口的类只用实现compare()方法呢?
其实稍加思索,就明白了:实现Comparator接口的类没有实现equals方法,是因为类里有equals()方法了,
所有的类都是从基类java.lang.Object中继承的,而Object类中已经有equals()方法了,
故而在你创建完成这个类,就已经实现了equals()方法了,是不是恍然大悟呢?!!!

 

User类的代码如下:

package test;

public class User {

	private String name;
	private Integer age;
	
	public User(String name, Integer age) {
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

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

	public Integer getAge() {
		return age;
	}

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

	@Override
	public String toString() {
		return "User [name=" + name + ", age=" + age + "]";
	}

}

  看下实现Comparator接口的类UserComparator的代码:

package test;

import java.util.Comparator;

public class UserComparator implements Comparator<User>{

	@Override
	public int compare(User o1, User o2) {
		int flag = o1.getName().compareTo(o2.getName());
		if(flag == 0) {
			flag = o1.getAge().compareTo(o1.getAge());
		}
		return flag;
	}

}

  接着我们要进行测试了哈!UserTest.java的代码如下:

package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class UserTest {
	
	public static void print(List<User> list) {
		for(User u : list) {
			System.out.println(u);
		}
	}
	
	public static void print(User[] array) {
		for(int i=0; i<array.length; i++) {
			System.out.println(array[i]);
		}
	}
	
	public static void main(String[] args) {
		
		List<User> users = new ArrayList<User>();
		users.add(new User("a",12));
		users.add(new User("C",25));
		users.add(new User("B",12));
		users.add(new User("A",15));
		users.add(new User("b",19));
		users.add(new User("c",17));
		users.add(new User("a",22));
		
		User[] users2 = new User[]{
				new User("a",12),
				new User("C",25),
				new User("B",12),
				new User("A",15),
				new User("b",19),
				new User("c",17),
				new User("a",22)
		};
		System.out.println("=========================Comparator接口的测试=========================");
		System.out.println("List排序前:");
		print(users);
		
		System.out.println();
		System.out.println();
		
		System.out.println("List排序后:");
		Collections.sort(users, new UserComparator());
		print(users);
		
		System.out.println("--------------------------------------------");
		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println();
		
		
		System.out.println("Array排序前:");
		print(users2);
		
		System.out.println();
		System.out.println();
		
		System.out.println("Array排序后:");
		Arrays.sort(users2, new UserComparator());
		print(users2);
		
	}

}

  测试的结果如下:

=========================Comparator接口的测试=========================
List排序前:
User [name=a, age=12]
User [name=C, age=25]
User [name=B, age=12]
User [name=A, age=15]
User [name=b, age=19]
User [name=c, age=17]
User [name=a, age=22]


List排序后:
User [name=A, age=15]
User [name=B, age=12]
User [name=C, age=25]
User [name=a, age=12]
User [name=a, age=22]
User [name=b, age=19]
User [name=c, age=17]
--------------------------------------------




Array排序前:
User [name=a, age=12]
User [name=C, age=25]
User [name=B, age=12]
User [name=A, age=15]
User [name=b, age=19]
User [name=c, age=17]
User [name=a, age=22]


Array排序后:
User [name=A, age=15]
User [name=B, age=12]
User [name=C, age=25]
User [name=a, age=12]
User [name=a, age=22]
User [name=b, age=19]
User [name=c, age=17]

  以上就是Comparator接口的学习了,快来一起学习吧!

以上是关于Comparable接口与Comparator接口的比较————Comparator接口详解的主要内容,如果未能解决你的问题,请参考以下文章

Comparable接口与Comparator接口的比较————Comparable接口详解

Comparable接口与Comparator接口的区别

comparator接口与Comparable接口的区别

comparator接口与Comparable接口的区别

comparator接口与Comparable接口的区别

comparator接口与Comparable接口的差别