前言:这种处理方式,在程序中偶尔会用的到,栗子很简单,关键是加强一下记忆,以及以备后用
1:实现Comparable接口的方式
1-1:没有使用泛型,重写compareTo()方法时需要判断类型及转换
public class Region implements Comparable{ /** * 区域ID */ private Integer id; /** * 区域名称 */ private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } /** * 方便打印对象信息,重写toString()方法 * @return */ @Override public String toString() { final StringBuffer sb = new StringBuffer("{\"Region\":{"); sb.append("\"id\":\"").append(id).append("\"").append(","); sb.append("\"name\":\"").append(name).append("\""); sb.append("}}"); return sb.toString(); } /** * 重写排序的方法,本例的核心方法 * @param o * @return */ @Override public int compareTo(Object o) { if (o instanceof Region) { Region r = (Region) o; if (this.getId() > r.getId()) { return 1; } else { return -1; } } else { throw new ClassCastException("Can‘t compare"); } } }
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class TestOrderMain { public static void main(String args[]){ List<Region> list = new ArrayList<Region>(); //构造一个无序的集合 Region region1 = new Region(); region1.setId(1); region1.setName("华东"); list.add(region1); Region region2 = new Region(); region2.setId(3); region2.setName("华北"); list.add(region2); Region region3 = new Region(); region3.setId(2); region3.setName("华南"); list.add(region3); //排序 Collections.sort(list); //出入排序后的集合 for(Region region : list){ System.out.println(region.toString()); } } }
1-2:使用了泛型,重写compareTo()方法时无需判断类型及转换,相对简单一些
public class Region implements Comparable<Region>{ /** * 区域ID */ private Integer id; /** * 区域名称 */ private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } /** * 方便打印对象信息,重写toString()方法 * @return */ @Override public String toString() { final StringBuffer sb = new StringBuffer("{\"Region\":{"); sb.append("\"id\":\"").append(id).append("\"").append(","); sb.append("\"name\":\"").append(name).append("\""); sb.append("}}"); return sb.toString(); } /** *重写排序的方法,本例的核心方法 * @param o * @return */ @Override public int compareTo(Region o) { return this.getId().compareTo(o.getId()); } }
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class TestOrderMain { public static void main(String args[]){ List<Region> list = new ArrayList<Region>(); //构造一个无序的集合 Region region1 = new Region(); region1.setId(1); region1.setName("华东"); list.add(region1); Region region2 = new Region(); region2.setId(3); region2.setName("华北"); list.add(region2); Region region3 = new Region(); region3.setId(2); region3.setName("华南"); list.add(region3); //排序 Collections.sort(list); //出入排序后的集合 for(Region region : list){ System.out.println(region.toString()); } } }
2:实现Comparator接口
2-1:这种方式比较好,在排序的时候直接使用Comparator的匿名对象,排序的方式也一目了然,并且和集合中的元素彻底解耦
public class Region{ /** * 区域ID */ private Integer id; /** * 区域名称 */ private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } /** * 方便打印对象信息,重写toString()方法 * @return */ @Override public String toString() { final StringBuffer sb = new StringBuffer("{\"Region\":{"); sb.append("\"id\":\"").append(id).append("\"").append(","); sb.append("\"name\":\"").append(name).append("\""); sb.append("}}"); return sb.toString(); } }
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class TestOrderMain { public static void main(String args[]){ List<Region> list = new ArrayList<Region>(); //构造一个无序的集合 Region region1 = new Region(); region1.setId(1); region1.setName("华东"); list.add(region1); Region region2 = new Region(); region2.setId(3); region2.setName("华北"); list.add(region2); Region region3 = new Region(); region3.setId(2); region3.setName("华南"); list.add(region3); //排序 Collections.sort(list,new Comparator<Region>(){ public int compare(Region o1, Region o2) { return o1.getId().compareTo(o2.getId()); } }); //出入排序后的集合 for(Region region : list){ System.out.println(region.toString()); } } }
2-2:实现Comparator接口,排序的时候直接传入对应的排序元素的匿名对象,估计也能使用泛型的形式,这个自己可以实验一下
import java.util.Comparator; public class Region implements Comparator{ /** * 区域ID */ private Integer id; /** * 区域名称 */ private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } /** * 方便打印对象信息,重写toString()方法 * @return */ @Override public String toString() { final StringBuffer sb = new StringBuffer("{\"Region\":{"); sb.append("\"id\":\"").append(id).append("\"").append(","); sb.append("\"name\":\"").append(name).append("\""); sb.append("}}"); return sb.toString(); } /** * 重写排序的方法,本例的核心方法 * @param o1 * @param o2 * @return */ @Override public int compare(Object o1, Object o2) { if (o1 instanceof Region && o2 instanceof Region) { Region r1 = (Region) o1; Region r2 = (Region) o2; if (r1.getId() > r2.getId()) { return 1; } else { return -1; } } else { throw new ClassCastException("Can‘t compare"); } } }
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class TestOrderMain { public static void main(String args[]){ List<Region> list = new ArrayList<Region>(); //构造一个无序的集合 Region region1 = new Region(); region1.setId(1); region1.setName("华东"); list.add(region1); Region region2 = new Region(); region2.setId(3); region2.setName("华北"); list.add(region2); Region region3 = new Region(); region3.setId(2); region3.setName("华南"); list.add(region3); //排序 Collections.sort(list,new Region()); //出入排序后的集合 for(Region region : list){ System.out.println(region.toString()); } } }
原文:http://www.cnblogs.com/godtrue/p/6375849.html