Array.sort()、Collections.sort()以及Comparable、Comparator学习笔记
Arrays.sort(T[], Comparator < ? super T > c) 方法用于对象数组按用户自定义规则排序。
Collections.sort(List<T>, Comparator < ? super T > c)方法用于对象集合按用户自定义规则排序。
所以当对象本身实现了Comparable接口,那么Arrays.sort(T[])就可以了。同理Collections.sort(List<T>)就可以了。
还是代码来的直接:
下面看对象自身实现了Comparable接口的方式:
Comparable用在对象本身,说明这个对象是可以被比较的,也就是说可以被排序的。(String和Integer之所以可以比较大小,是因为它们都实现了Comparable接口,并实现了compareTo()方法)。
Compator用在对象外,相当于定义了一个排序算法。
还是代码来的直接:
package
com.zx.ww.comparable;
import java.util.Arrays;
import java.util.Comparator;
public class ComparatorTest {
public static void main(String[] args) {
Dog d1 = new Dog( 2 );
Dog d2 = new Dog( 1 );
Dog d3 = new Dog( 3 );
Dog[] dogArray = {d1, d2, d3};
printDogs(dogArray);
Arrays.sort(dogArray, new DogSizeComparator());
printDogs(dogArray);
}
public static void printDogs(Dog[] dogArray) {
for (Dog dog : dogArray) {
System.out.print(dog.size + " " );
}
System.out.println();
}
}
class Dog{
int size;
public Dog( int size) {
this .size = size;
}
}
class DogSizeComparator implements Comparator < Dog > {
@Override
public int compare(Dog dog1, Dog dog2) {
// TODO Auto-generated method stub
return dog1.size - dog2.size;
}
}
输出结果:import java.util.Arrays;
import java.util.Comparator;
public class ComparatorTest {
public static void main(String[] args) {
Dog d1 = new Dog( 2 );
Dog d2 = new Dog( 1 );
Dog d3 = new Dog( 3 );
Dog[] dogArray = {d1, d2, d3};
printDogs(dogArray);
Arrays.sort(dogArray, new DogSizeComparator());
printDogs(dogArray);
}
public static void printDogs(Dog[] dogArray) {
for (Dog dog : dogArray) {
System.out.print(dog.size + " " );
}
System.out.println();
}
}
class Dog{
int size;
public Dog( int size) {
this .size = size;
}
}
class DogSizeComparator implements Comparator < Dog > {
@Override
public int compare(Dog dog1, Dog dog2) {
// TODO Auto-generated method stub
return dog1.size - dog2.size;
}
}
2
1
3
1 2 3
这是对象数组用了Comparator的结果。1 2 3
下面看对象自身实现了Comparable接口的方式:
/**
*
*/
package com.zx.ww.comparable;
import java.util.Arrays;
/**
* @author wuwei
* 2014年9月29日
*/
public class User implements Comparable < Object > {
private int id;
private String name;
private int age;
public User( int id, String name, int age) {
this .id = id;
this .name = name;
this .age = age;
}
public int getId() {
return id;
}
public void setId( int id) {
this .id = id;
}
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;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
return this .age - ((User)o).getAge();
}
public static void main(String[] args) {
User[] users = new User[] { new User( 1 , " zhangsan " , 28 ), new User( 2 , " lisi " , 25 )};
Arrays.sort(users);
for ( int i = 0 ; i < users.length; i ++ ) {
User user = users[i];
System.out.println(user.getId() + " " + user.getAge());
}
}
}
输出结果:*
*/
package com.zx.ww.comparable;
import java.util.Arrays;
/**
* @author wuwei
* 2014年9月29日
*/
public class User implements Comparable < Object > {
private int id;
private String name;
private int age;
public User( int id, String name, int age) {
this .id = id;
this .name = name;
this .age = age;
}
public int getId() {
return id;
}
public void setId( int id) {
this .id = id;
}
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;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
return this .age - ((User)o).getAge();
}
public static void main(String[] args) {
User[] users = new User[] { new User( 1 , " zhangsan " , 28 ), new User( 2 , " lisi " , 25 )};
Arrays.sort(users);
for ( int i = 0 ; i < users.length; i ++ ) {
User user = users[i];
System.out.println(user.getId() + " " + user.getAge());
}
}
}
2
25
1 28
上述都是Arrays.sort()的应用方式,同理Collections.sort()一样的实现,代码如下,比较简单:1 28
package
com.zx.ww.comparable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CollectionSortTest {
public static void main(String[] args) {
List < String > list = new ArrayList < String > ();
list.add( " ac " );
list.add( " ab " );
list.add( " aa " );
list.add( " db " );
list.add( " ca " );
for (String string : list) {
System.out.print(string + " " );
}
System.out.println();
Collections.sort(list);
for (String string : list) {
System.out.print(string + " " );
}
System.out.println();
// 对象A自身实现Comparable接口
List < A > aList = new ArrayList < A > ();
aList.add( new A( " wuwei " , 1 ));
aList.add( new A( " zhangsan " , 3 ));
aList.add( new A( " lisi " , 2 ));
for (A a : aList) {
System.out.print(a + " " );
}
System.out.println();
Collections.sort(aList);
for (A a : aList) {
System.out.print(a + " " );
}
System.out.println();
// 重写Conparator接口方法
List < B > bList = new ArrayList < B > ();
bList.add( new B( " wuwei " , 1 ));
bList.add( new B( " zhangsan " , 3 ));
bList.add( new B( " lisi " , 2 ));
for (B b : bList) {
System.out.print(b + " " );
}
System.out.println();
Collections.sort(bList, new Comparator < B > () {
@Override
public int compare(B b1, B b2) {
// TODO Auto-generated method stub
return b1.getCount().compareTo(b2.getCount());
}
});
for (B b : bList) {
System.out.print(b + " " );
}
System.out.println();
}
}
// 对象A自身实现Comparable接口
class A implements Comparable < A > {
private String name;
private Integer order;
public A(String name, Integer order) {
this .name = name;
this .order = order;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public Integer getOrder() {
return order;
}
public void setOrder(Integer order) {
this .order = order;
}
public String toString() {
return " name is " + name + " order is " + order;
}
@Override
public int compareTo(A o) {
// TODO Auto-generated method stub
return this .order.compareTo(o.getOrder());
}
}
class B{
private String name;
private Integer count;
public B(String name, Integer count) {
this .name = name;
this .count = count;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this .count = count;
}
public String toString() {
return " name is " + name + " count is " + count;
}
}
输出结果:import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CollectionSortTest {
public static void main(String[] args) {
List < String > list = new ArrayList < String > ();
list.add( " ac " );
list.add( " ab " );
list.add( " aa " );
list.add( " db " );
list.add( " ca " );
for (String string : list) {
System.out.print(string + " " );
}
System.out.println();
Collections.sort(list);
for (String string : list) {
System.out.print(string + " " );
}
System.out.println();
// 对象A自身实现Comparable接口
List < A > aList = new ArrayList < A > ();
aList.add( new A( " wuwei " , 1 ));
aList.add( new A( " zhangsan " , 3 ));
aList.add( new A( " lisi " , 2 ));
for (A a : aList) {
System.out.print(a + " " );
}
System.out.println();
Collections.sort(aList);
for (A a : aList) {
System.out.print(a + " " );
}
System.out.println();
// 重写Conparator接口方法
List < B > bList = new ArrayList < B > ();
bList.add( new B( " wuwei " , 1 ));
bList.add( new B( " zhangsan " , 3 ));
bList.add( new B( " lisi " , 2 ));
for (B b : bList) {
System.out.print(b + " " );
}
System.out.println();
Collections.sort(bList, new Comparator < B > () {
@Override
public int compare(B b1, B b2) {
// TODO Auto-generated method stub
return b1.getCount().compareTo(b2.getCount());
}
});
for (B b : bList) {
System.out.print(b + " " );
}
System.out.println();
}
}
// 对象A自身实现Comparable接口
class A implements Comparable < A > {
private String name;
private Integer order;
public A(String name, Integer order) {
this .name = name;
this .order = order;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public Integer getOrder() {
return order;
}
public void setOrder(Integer order) {
this .order = order;
}
public String toString() {
return " name is " + name + " order is " + order;
}
@Override
public int compareTo(A o) {
// TODO Auto-generated method stub
return this .order.compareTo(o.getOrder());
}
}
class B{
private String name;
private Integer count;
public B(String name, Integer count) {
this .name = name;
this .count = count;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this .count = count;
}
public String toString() {
return " name is " + name + " count is " + count;
}
}
ac ab aa db ca
aa ab ac ca db
name is wuwei order is 1 name is zhangsan order is 3 name is lisi order is 2
name is wuwei order is 1 name is lisi order is 2 name is zhangsan order is 3
name is wuwei count is 1 name is zhangsan count is 3 name is lisi count is 2
name is wuwei count is 1 name is lisi count is 2 name is zhangsan count is 3
aa ab ac ca db
name is wuwei order is 1 name is zhangsan order is 3 name is lisi order is 2
name is wuwei order is 1 name is lisi order is 2 name is zhangsan order is 3
name is wuwei count is 1 name is zhangsan count is 3 name is lisi count is 2
name is wuwei count is 1 name is lisi count is 2 name is zhangsan count is 3