java 中的2个接口 Comparable和Comparator
像Integer、String這些類型的數(shù)據(jù)都是已經(jīng)實現(xiàn)Comparable接口的,所以對這些類型可以直接通過Arrays.sort(...)和Collections.sort(...)方法進行排序。但是對于一些自己new出來的對象而言,如果想使用sort這種方法,必須要實現(xiàn)Comparable接口,這是由sort方法底層決定的,具體看一下實例:
定義一個Bean
public class Employee implements Comparable{ ?
private int age; ?private String name; ?
public Employee(String name,int age){ ?
?this.name=name; ??
this.age=age; ?
} ?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 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; ??
Employee other = (Employee) 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 "Employee [age=" + age + ", name=" + name + "]"; ?
} ?
@Override ?
public int compareTo(Object o) { ?
?if(o instanceof Employee){ ?
?if(age > ((Employee) o).age){ ??
?return 1; ??
}else if(age == ((Employee) o).age){
???return 0; ??}else if (age < ((Employee) o).age){
???return -1; ??} ?? ??
} ??return 0; ?
} ?
}
//
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class EmployeeSortTest { ?
public static void main(String[] args){ ?
Employee[] staff = new Employee[3]; ?
staff[0]= new Employee("aaa",12); ?
staff[1] = new Employee("bbb",13); ?
staff[2] = new Employee("ccc",14); ?
Arrays.sort(staff);//sort方法可以實現(xiàn)對對象數(shù)組的排序,但必須實現(xiàn)Comparable接口 ?
for(Employee e: staff){ ??
System.out.println(e); ?
} ?//以上方法雖然可以實現(xiàn)對象的排序,但是有弊端;
?//1、修改了公共類Employee,而很多情況下我們是沒有辦法修改公共的類, ?
//2、當我們需求滿足不同的方式進行排序時,這種方法是不可行的 ?
//接下來java中提供了Comparator接口,Comparator使用其compare()方法返回的整數(shù)來比較2個對象, ?
//這樣就無需修改公共類 ?
System.out.println("---------------"); ?
List<Employee> list = new ArrayList<Employee> (); ?
list.add(new Employee("aaa",12)); ?
list.add(new Employee("bbb",13)); ?
list.add(new Employee("bbb",14)); ?
Collections.sort(list, new Comparator<Employee>(){
??@Override ??
public int compare(Employee? o1, Employee? o2) { ???
// TODO Auto-generated method stub ???
return o1.getAge()-o2.getAge(); ?
?} ?? ?
}); ?
for(Employee e : list){ ??
System.out.println(e); ?
} ?
}
}
//輸出結果:
Employee [age=12, name=aaa]
Employee [age=13, name=bbb]
Employee [age=14, name=ccc]
Employee [age=12, name=aaa]
Employee [age=13, name=bbb]
Employee [age=14, name=bbb]
轉載于:https://www.cnblogs.com/muliu/p/5502330.html
總結
以上是生活随笔為你收集整理的java 中的2个接口 Comparable和Comparator的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Swift -- 6.函数和闭包
- 下一篇: 原创:保加利亚是怎么丢失地中海沿岸的?