Tools中是排序的实现过程,用了一个泛型参数T
import java.util.ArrayList;
public class Tools<T>
{
public void sortlist(ArrayList<T> list, Bijiaoqi<T> bijiaoqi)
{
for (int i=0; i<list.size()-1; i++)
{
for (int j=0; j<list.size()-i-1; j++)
{
if (bijiaoqi.bijiao(list.get(j), list.get(j+1)))
{
T tmp = list.get(j);
list.set(j, list.get(j+1));
list.set(j+1, tmp);
}
}
}
}
}
Bijiaoqi是一个接口
public interface Bijiaoqi<T>
{
public boolean bijiao(T t1,T t2);
}
Age是Bijiaoqi接口的一个实现,如果需要比别的属性,就写一个新的实现就行
public class Age implements Bijiaoqi<User>
{
@Override
public boolean bijiao(User u1, User u2)
{
if (u1.getAge() < u2.getAge())// TODO Auto-generated method stub
{
return true;
}
return false;
}
}
一个User类
public class User
{
private String id;
private String name;
private int age;
private float salary;
public User(String id, String name, int age, float salary)
{
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public String getId()
{
return id;
}
public void setId(String 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;
}
public float getSalary()
{
return salary;
}
public void setSalary(float salary)
{
this.salary = salary;
}
public String toString()
{
return this.name+";"+this.age+";"+this.salary;
}
}
main入口
public class test
{
public static void main(String[] args)
{
ArrayList<User> list1 = new ArrayList<User>();
User u1 = new User("1", "a", 10, 100);
User u2 = new User("2", "b", 5, 200);
User u3 = new User("3", "c", 17, 300);
User u4 = new User("4", "d", 6, 5000);
list1.add(u1);
list1.add(u2);
list1.add(u3);
list1.add(u4);
Tools tools = new Tools();
Bijiaoqi age = new Age();
tools.sortlist(list1, new Age());
for (User u:list1)
{
System.out.println(u);
}
}
}
这里也可以使用匿名内部类来传递比较器对象,就不用重新写个类去实现接口,直接在调用的时候写实现。
public class test
{
public static void main(String[] args)
{
ArrayList<User> list1 = new ArrayList<User>();
User u1 = new User("1", "a", 10, 100);
User u2 = new User("2", "b", 5, 200);
User u3 = new User("3", "c", 17, 300);
User u4 = new User("4", "d", 6, 5000);
list1.add(u1);
list1.add(u2);
list1.add(u3);
list1.add(u4);
Tools tools = new Tools();
Bijiaoqi age = new Age();
tools.sortlist(list1, new Bijiaoqi<User>()
{
@Override
public boolean bijiao(User u1, User u2)
{
if (u1.getAge()<u2.getAge())
{
return true;
}
return false;
}
});
for (User u:list1)
{
System.out.println(u);
}
}
}
============================================================
使用Collections自带的排序方法,一种方法是参数中new一个匿名内部类Comparator
public class test
{
public static void main(String[] args)
{
ArrayList<User> list1 = new ArrayList<User>();
User u1 = new User("1", "a", 10, 100);
User u2 = new User("2", "b", 5, 200);
User u3 = new User("3", "c", 17, 300);
User u4 = new User("4", "d", 6, 5000);
list1.add(u1);
list1.add(u2);
list1.add(u3);
list1.add(u4);
Collections.sort(list1, new Comparator<User>()
{
@Override
public int compare(User u1, User u2)
{
if (u1.getAge()<u2.getAge())
{
return 1;
}
return -1;
}
});
for (User u:list1)
{
System.out.println(u);
}
}
}
或者让list中的对象自身能够比较大小(重构Comparable接口的compareTo方法)
public class User implements Comparable<User>
{
public User(String id, String name, int age, float salary)
{
public int compareTo(User other)
{
if (this.getAge()>other.getAge())
{
return 1;
}
return -1;
}
}
public class test
{
public static void main(String[] args)
{
ArrayList<User> list1 = new ArrayList<User>();
User u1 = new User("1", "a", 10, 100);
User u2 = new User("2", "b", 5, 200);
User u3 = new User("3", "c", 17, 300);
User u4 = new User("4", "d", 6, 5000);
list1.add(u1);
list1.add(u2);
list1.add(u3);
list1.add(u4);
Collections.sort(list1);
for (User u:list1)
{
System.out.println(u);
}
}
}