1、关于Compare接口接口的比较
以一个简单的例子来说,将一个项目分成三个部分,第一个pojo为要比较的对象类,第二个定义一个Comparator的类去实现comparator的接口,内部覆写compare的方法,第三个为java的测试类,主要是写Main相关。
一个简单的例子说明一切,本例用于比较User的对象,而不是String或者Integer这两个已经实现Comparable接口的对象。
//一个POJO例子
class User {
String name;
String age;
public User(String name,String age){
this.name=name;
this.age=age;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//具体的比较类,实现Comparator接口
import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class ComparatorUser implements Comparator{
public int compare(Object arg0, Object arg1) {
User user0=(User)arg0;
User user1=(User)arg1;
//首先比较年龄,如果年龄相同,则比较名字
int flag=user0.getAge().compareTo(user1.getAge());
if(flag==0){
return user0.getName().compareTo(user1.getName());
}else{
return flag;
}
}
}
//测试类
public class SortTest {
public static void main(String[] args){
List userlist=new ArrayList();
userlist.add(new User("dd","4"));
userlist.add(new User("aa","1"));
userlist.add(new User("ee","5"));
userlist.add(new User("bb","2"));
userlist.add(new User("ff","5"));
userlist.add(new User("cc","3"));
userlist.add(new User("gg","6"));
ComparatorUser comparator=new ComparatorUser();
Collections.sort(userlist, comparator);
for (int i=0;i
System.out.println(user_temp.getAge()+","+user_temp.getName());
}
}
}
//首先年龄排序,如果年龄相同,则按名字排序
结果:
1, aa
2, bb
3, cc
4, dd
5, ee //注意:同样是5岁的人,则比较名字(ee,ff),然后排序
5, ff
6, gg
2、关于Comparable<泛型>
本方法相对于上面的方法更加简单,主要是覆写了本类的CompareTo()方法,只要在本类的类名上去implements comparable<泛型>接口,此处的泛型指代要进行操作比较的对象类型,在类的中间覆写CompareTo()方法,则最后在Main中直接调用Collectins.sort()即可自动调用自己所写的compareTo()方法即可。
一个简单的例子说明一切
//一个简单的pojo类
package org.employee.pojo;
public class Employee implements Comparable
private String name;
private Double basicSalary;
public Employee() {
// TODO Auto-generated constructor stub
}
public Employee(String name, Double basicSalary) {
super();
this.name = name;
this.basicSalary = basicSalary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getBasicSalary() {
return basicSalary;
}
public void setBasicSalary(Double basicSalary) {
this.basicSalary = basicSalary;
}
public Double getSalary(){
return this.basicSalary;
}
public Double getGongjijin(){
return this.getSalary()*0.1;
}
@Override
public int compareTo(Employee e){
return (int)(this.getSalary()-e.getSalary());
}
@Override
public String toString() {
return "员工信息 [姓名:" + name + ", 工资" + this.getSalary() + "]";
}
}
//Main类
package org.employee.main;
import java.util.ArrayList;
import java.util.Collections;
import org.employee.pojo.Employee;
import org.employee.pojo.Manager;
public class TestMain {
public static void main(String[] args) {
ArrayList
Employee employee1=new Employee("张三",3000.0);
employees.add(employee1);
employee1=new Employee("张四",2000.0);
employees.add(employee1);
employee1=new Employee("张五",5000.0);
employees.add(employee1);
employee1=new Employee("张六",4000.0);
employees.add(employee1);
employee1=new Employee("张七",3500.0);
employees.add(employee1);
System.out.println("普通员工的工资排序:");
Collections.sort(employees);
for(int i=0;i<5;i++){
System.out.println(employees.get(i));
}
}
}
结果: