使用Comparator比較器進行排序
題目:
地址類:
package api1_9作業;
import java.util.ArrayList;
public class Address {
private String name;
private String street;
private String city;
private String country;
private String pinCode;
/*
* 姓名、街道、市(縣)、省(自治區)、國家(地區)和程式碼
*
* */
public Address(String name, String street, String city, String country, String pinCode) {
this.name = name;
this.street = street;
this.city = city;
this.country = country;
this.pinCode = pinCode;
}
public Address() {
super();
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry () {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getPinCode() {
return pinCode;
}
public void setPinCode(String pinCode) {
this.pinCode = pinCode;
}
/*5. 將第三題中的ArrayList實現排序。
a)提示:排序必須先實現該類物件能比較大小。
b)地址先按比較國家,相同則比較城市,再相同則比較街道,再相同則比較姓名
重寫toString方法
*/
@Override
public String toString()
{
return "\nname:"+name+"\nstreet:"+street+"\ncity:"+city+"\ncountry:"+country+"\npinCode:"+pinCode+"\n";
}
}
實現比較:
package api1_9作業;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;
public class demo2 {
public static void main(String[] args) {
Address str1 = new Address("David Clarke","10 Downing Street","LondonState:London",
"United Kindom","110022");
Address str2 = new Address ("John Lenon","12 Park Avenue","CaliforniaState:California",
"USA ","210033");
Address str3 = new Address ("Stefii Graff","14 Maple lane","MahomaState:Sydney",
"Australia","412033");
ArrayList<Address> arr = new ArrayList<Address>();
arr.add(str1);
arr.add(str2);
arr.add(str3);
//比較器比較
Collections.sort(arr, new Comparator<Address>() {
/*b)地址先按比較國家,相同則比較城市,再相同則比較街道,再相同則比較姓名
* */
/* * Stefii Graff","14 Maple lane","Sydney", "Australia","412033"
* 姓名 街道 城市 國家 程式碼
* 地址先按比較國家,相同則比較城市,再相同則比較街道,再相同則比較姓名
* this.name = name;
this.street = street;
this.city = city;
this.country = country;
this.pinCode = pinCode;
都升序比較
* */
@Override
public int compare(Address o1, Address o2) {
/*
*
* */
int result = o1.getCountry().charAt(0)-o2.getCountry().charAt(0);//國家升序
if(o1.getCountry().equals(o2.getCountry())) {//如果國家相同 比較城市
result = o1.getCity().charAt(0)-o2.getCity().charAt(0);//城市升序
if(o1.getCity().equals(o2.getCity())) {//如果城市相同 比較街道
result = o1.getStreet().charAt(0)-o2.getStreet().charAt(0);
if(o1.getStreet().equals(o2.getStreet())) {//如果街道相同 比較姓名
result = o1.getName().charAt(0)-o2.getName().charAt(0);
}
}
}
return result;
}
});
System.out.println(arr.toString());
}
}
說到排序了,簡單的說就是兩個物件之間比較大小,那麼在JAVA中提供了兩種比較實現的方式,一種是比較死板的採用java.lang.Comparable
介面去實現,一種是靈活的當我需要做排序的時候在去選擇的java.util.Comparator
介面完成。
那麼我們採用的public static <T> void sort(List<T> list)
這個方法完成的排序,實際上要求了被排序的型別需要實現Comparable介面完成比較的功能,在String型別上如下:
public final class String implements java.io.Serializable, Comparable<String>, CharSequence {
String類實現了這個介面,並完成了比較規則的定義,但是這樣就把這種規則寫死了,那比如我想要字串按照第一個字元降序排列,那麼這樣就要修改String的原始碼,這是不可能的了,那麼這個時候我們可以使用
public static <T> void sort(List<T> list,Comparator<? super T> )
方法靈活的完成,這個裡面就涉及到了Comparator這個介面,位於位於java.util包下,排序是comparator能實現的功能之一,該介面代表一個比較器,比較器具有可比性!顧名思義就是做排序的,通俗地講需要比較兩個物件誰排在前誰排在後,那麼比較的方法就是:
-
public int compare(String o1, String o2)
:比較其兩個引數的順序。兩個物件比較的結果有三種:大於,等於,小於。
如果要按照升序排序,
則o1 小於o2,返回(負數),相等返回0,01大於02返回(正數)
如果要按照降序排序
則o1 小於o2,返回(正數),相等返回0,01大於02返回(負數)
如果在使用的時候,想要獨立的定義規則去使用 可以採用Collections.sort(List list,Comparetor c)方式,自己定義規則:
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.getAge()-o1.getAge();//以學生的年齡降序
}
});