java中,对集合对象或者数组对象排序,有两种实现方式。
即:(1)对象实现Comparable 接口
(2)定义比较器,实现Comparator接口。
下面会简要介绍这两种方法的区别,并附上实现代码,供大家参考。
Comparable介绍
Comparable是在集合内部定义的方法实现的排序,位于java.lang下。
Comparable 接口仅仅只包括一个函数,它的定义如下:
- package java.lang;
- import java.util.*;
- public interface Comparable<T> {
- public int compareTo(T o);
- }
package java.lang;
import java.util.*;
public interface Comparable<T> {
public int compareTo(T o);
}
若x.compareTo(y) <0,则x<y;若x.compareTo(y) =0,则x=y;若x.compareTo(y) >0,则x=y;
Comparable是一个对象,本身就已经支持自比较所需要实现的接口。
自定义类要在加入list容器中后能够排序,也可以实现Comparable接口。
在用Collections类的sort方法排序时若不指定Comparator,那就以自然顺序排序。所谓自然顺序就是实现Comparable接口设定的排序方式。
若一个类实现了comparable接口,则意味着该类支持排序。如String、Integer自己就实现了Comparable接口,可完成比较大小操作。
一个已经实现comparable的类的对象或数据,可以通过Collections.sort(list) 或者Arrays.sort(arr)实现排序。通过Collections.sort(list,Collections.reverseOrder());对list进行倒序排列。
Comparator介绍
Comparator是在集合外部实现的排序,位于java.util下。
Comparator接口包含了两个函数。
- package java.util;
- public interface Comparator<T> {
- int compare(T o1, T o2);
- boolean equals(Object obj);
- }
package java.util;
public interface Comparator<T> {
int compare(T o1, T o2);
boolean equals(Object obj);
}
我们若需要控制某个类的次序,而该类本身不支持排序(即没有实现Comparable接口);那么,我们可以新建一个该类的比较器来进行排序。这个比较器只需要实现comparator即可。
如果引用的为第三方jar包,这时候,没办法改变类本身,可是使用这种方式。
Comparator是一个专用的比较器,当这个对象不支持自比较或者自比较函数不能满足要求时,可写一个比较器来完成两个对象之间大小的比较。
Comparator体现了一种策略模式(strategy design pattern),就是不改变对象自身,而用一个策略对象(strategy object)来改变它的行为。
comparable相当于内部比较器。comparator相当于外部比较器。
comparable实例
- package test;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- public class test {
- public static void main(String[] args) {
- List<UserInfo> list = new ArrayList<UserInfo>();
- list.add(new UserInfo(1,21,“name1”));
- list.add(new UserInfo(2,27,“name1”));
- list.add(new UserInfo(3,15,“name1”));
- list.add(new UserInfo(5,24,“name1”));
- list.add(new UserInfo(4,24,“name1”));
- //对该类排序
- Collections.sort(list);
- for(int i=0;i<list.size();i++){
- System.out.println(list.get(i));
- }
- }
- }
- class UserInfo implements Comparable<UserInfo>{
- private int userid;
- private int age;
- private String name;
- public UserInfo(int userid, int age, String name) {
- this.userid = userid;
- this.age = age;
- this.name = name;
- }
- public int getUserid() {
- return userid;
- }
- public void setUserid(int userid) {
- this.userid = userid;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString(){
- return this.userid+“,”+this.age+“,”+this.name;
- }
- @Override
- public int compareTo(UserInfo o) {
- //如果年龄相同,则比较userid,也可以直接 return this.age-o.age;
- if(this.age-o.age==0){
- return this.userid-o.userid;
- }else{
- return this.age-o.age;
- }
- }
- }
package test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class test {
public static void main(String[] args) {
List<UserInfo> list = new ArrayList<UserInfo>();
list.add(new UserInfo(1,21,"name1"));
list.add(new UserInfo(2,27,"name1"));
list.add(new UserInfo(3,15,"name1"));
list.add(new UserInfo(5,24,"name1"));
list.add(new UserInfo(4,24,"name1"));
//对该类排序
Collections.sort(list);
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}
class UserInfo implements Comparable<UserInfo>{
private int userid;
private int age;
private String name;
public UserInfo(int userid, int age, String name) {
this.userid = userid;
this.age = age;
this.name = name;
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString(){
return this.userid+","+this.age+","+this.name;
}
@Override
public int compareTo(UserInfo o) {
//如果年龄相同,则比较userid,也可以直接 return this.age-o.age;
if(this.age-o.age==0){
return this.userid-o.userid;
}else{
return this.age-o.age;
}
}
}
comparator实例
- package test;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- public class test1 {
- public static void main(String[] args) {
- List<UserInfo> list = new ArrayList<UserInfo>();
- list.add(new UserInfo(1,21,“name1”));
- list.add(new UserInfo(2,27,“name2”));
- list.add(new UserInfo(3,15,“name3”));
- list.add(new UserInfo(5,24,“name4”));
- list.add(new UserInfo(4,24,“name5”));
- //new一个比较器
- MyComparator comparator = new MyComparator();
- //对list排序
- Collections.sort(list,comparator);
- for(int i=0;i<list.size();i++){
- System.out.println(list.get(i));
- }
- }
- }
- class MyComparator implements Comparator<UserInfo>{
- @Override
- public int compare(UserInfo o1,UserInfo o2) {
- if(o1.getAge()-o2.getAge()==0){
- return o1.getUserid()-o2.getUserid();
- }else{
- return o1.getAge()-o2.getAge();
- }
- }
- }
- class UserInfo{
- private int userid;
- private int age;
- private String name;
- public UserInfo(int userid, int age, String name) {
- this.userid = userid;
- this.age = age;
- this.name = name;
- }
- public int getUserid() {
- return userid;
- }
- public void setUserid(int userid) {
- this.userid = userid;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString(){
- return this.userid+“,”+this.age+“,”+this.name;
- }
- }
package test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class test1 {
public static void main(String[] args) {
List<UserInfo> list = new ArrayList<UserInfo>();
list.add(new UserInfo(1,21,"name1"));
list.add(new UserInfo(2,27,"name2"));
list.add(new UserInfo(3,15,"name3"));
list.add(new UserInfo(5,24,"name4"));
list.add(new UserInfo(4,24,"name5"));
//new一个比较器
MyComparator comparator = new MyComparator();
//对list排序
Collections.sort(list,comparator);
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}
class MyComparator implements Comparator<UserInfo>{
@Override
public int compare(UserInfo o1,UserInfo o2) {
if(o1.getAge()-o2.getAge()==0){
return o1.getUserid()-o2.getUserid();
}else{
return o1.getAge()-o2.getAge();
}
}
}
class UserInfo{
private int userid;
private int age;
private String name;
public UserInfo(int userid, int age, String name) {
this.userid = userid;
this.age = age;
this.name = name;
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString(){
return this.userid+","+this.age+","+this.name;
}
}
</div>