封装一个Person类型到List中
Person{
private int id;
private String name;
private String age;
}
用策略模式实现id、name、age的升序和降序的排序,如果name相同那么按id的升序来排序,如果age相同,那么按id的升序来排序。
第一种方法:
//定义一个排序策略模式接口
public interface Strategy {
public void sort(List<Person> list);
}
//定义一个环境
public class Person {
private static int nextid =10001;
private int id;
private String name;
private int age;
public Person(String name,int age){
id = nextid++;
this.name = name;
this.age = age;
}
public int getId() {
return 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;
}
//按id的升序排序,默认也是升序的
public class SortByIdAsc implements Strategy{
@Override
public void sort(List<Person> list) {
Collections.sort(list, new MyIdComparator());
}
//自然顺序排序
private class MyIdComparator implements Comparator<Person>{
@Override
public int compare(Person o1, Person o2) {
int id1 =o1.getId();
int id2 = o2.getId();
return id1-id2;//哪个值小,就排在前面,如果这是负数,那么会升序
}
}
}
//按id的降序排序
public class SortByIdDesc implements Strategy{
@Override
public void sort(List<Person> list) {
Collections.sort(list, new MyIdComparator());
}
//倒序排序
private class MyIdComparator implements Comparator<Person>{
@Override
public int compare(Person o1, Person o2) {
int id1 =o1.getId();
int id2 = o2.getId();
return id2-id1;
}
}
}
//按姓名的升序排序
public class SortByNameAsc implements Strategy{
@Override
public void sort(List<Person> list) {
Collections.sort(list,new MyNameComparator());
}
//名字按字典顺序的升序排列,如果名字相同,则按id的升序排列
private class MyNameComparator implements Comparator<Person>{
@Override
public int compare(Person o1, Person o2) {
String name1 = o1.getName();
String name2 = o2.getName();
if(name1.equals(name2)){
return o1.getId() - o2.getId();
}
return name1.compareTo(name2);
}
}
}
//按姓名的降序排序
public class SortByNameDesc implements Strategy{
@Override
public void sort(List<Person> list) {
Collections.sort(list,new MyNameComparator());
}
//名字按字典顺序的降序排列,如果名字相同,则按id的升序排列
private class MyNameComparator implements Comparator<Person>{
@Override
public int compare(Person o1, Person o2) {
String name1 = o1.getName();
String name2 = o2.getName();
if(name1.equals(name2)){
return o1.getId() - o2.getId();
}
return name2.compareTo(name1);
}
}
}
//客户端调用
public class Client {
public static void main(String[] args){
List<Person> list = new ArrayList<Person>();
list.add(new Person("hacker",18));
list.add(new Person("hackjava",22));
list.add(new Person("李美丽",22));
list.add(new Person("hacker",20));
list.add(new Person("wuhen",21));
list.add(new Person("dalixiaowen",56));
Strategy idasc = new SortByIdAsc();
idasc.sort(list);
System.out.println("按id的升序排列");
System.out.println("id name age");
for(Iterator<Person> iter = list.iterator();iter.hasNext();){
Person p = iter.next();
System.out.println(p.getId()+" "+p.getName()+" "+p.getAge());
}
System.out.println("-------------------------------");
Strategy iddesc = new SortByIdDesc();
iddesc.sort(list);
System.out.println("按id的降序排列");
System.out.println("id name age");
for(Iterator<Person> iter = list.iterator();iter.hasNext();){
Person p = iter.next();
System.out.println(p.getId()+" "+p.getName()+" "+p.getAge());
}
System.out.println("-------------------------------");
Strategy nameasc = new SortByNameAsc();
nameasc.sort(list);
System.out.println("按name的升序排列");
System.out.println("id name age");
for(Iterator<Person> iter = list.iterator();iter.hasNext();){
Person p = iter.next();
System.out.println(p.getId()+" "+p.getName()+" "+p.getAge());
}
System.out.println("-------------------------------");
Strategy namedesc = new SortByNameDesc();
namedesc.sort(list);
System.out.println("按name的降序排列");
System.out.println("id name age");
for(Iterator<Person> iter = list.iterator();iter.hasNext();){
Person p = iter.next();
System.out.println(p.getId()+" "+p.getName()+" "+p.getAge());
}
}
}
第二中方法:
排序策略接口
public interface SortInterface
{
public void sort(List<Person> list);
}
定义一个要排序的对象
public class Person
{
private int id;
private String name;
private int 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;
}
}
//按名字升序排序
public class UpNameSort implements SortInterface, Comparator<Person>
{
public void sort(List<Person> list)
{
Collections.sort(list, this);
}
public int compare(Person o1, Person o2)
{
int result = o1.getName().compareTo(o2.getName());
if(0 == result)
{
return o1.getId() - o2.getId();
}
return result;
}
}
//按名字降序排序
public class DownNameSort implements SortInterface, Comparator<Person>
{
public void sort(List<Person> list)
{
Collections.sort(list, this);
}
public int compare(Person o1, Person o2)
{
int result = o2.getName().compareTo(o1.getName());
if(0 == result)
{
return o1.getId() - o2.getId();
}
return result;
}
}
//环境角色
public class Environment
{
private SortInterface sortInterface;
public Environment(SortInterface sortInterface)
{
this.sortInterface = sortInterface;
}
public Environment()
{
}
public void setSortInterface(SortInterface sortInterface)
{
this.sortInterface = sortInterface;
}
public void sort(List<Person> list)
{
this.sortInterface.sort(list);
}
}
//客户端调用
public class Client
{
public static void main(String[] args)
{
Person p1 = new Person();
p1.setName("Tom");
p1.setId(1);
p1.setAge(20);
Person p2 = new Person();
p2.setName("Tonny");
p2.setId(2);
p2.setAge(50);
Person p3 = new Person();
p3.setName("Tom");
p3.setId(5);
p3.setAge(30);
Person p4 = new Person();
p4.setName("ABC");
p4.setId(8);
p4.setAge(10);
Person p5 = new Person();
p5.setName("Xyz");
p5.setId(9);
p5.setAge(15);
List<Person> list = new ArrayList<Person>();
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
list.add(p5);
Environment env = new Environment();
UpNameSort uns = new UpNameSort();
env.setSortInterface(uns);
env.sort(list);
for (int i = 0; i < list.size(); i++)
{
Person p = list.get(i);
System.out.println("id: " + p.getId() + ", name: " + p.getName()
+ ", age:" + p.getAge());
}
System.out.println("--------------");
DownNameSort dns = new DownNameSort();
env.setSortInterface(dns);
env.sort(list);
for (int i = 0; i < list.size(); i++)
{
Person p = list.get(i);
System.out.println("id: " + p.getId() + ", name: " + p.getName()
+ ", age:" + p.getAge());
}
}
}