Java中的排序

来源:互联网 发布:linux命令手册 pdf 编辑:程序博客网 时间:2024/06/12 21:48

    Java的排序从“容器”类型来看,主要包括两种类型:一是数组排序;二是collection排序。

    从被排序的元素来看,又主要分为两类:一是基本数据类型的排序;而是自定义对象的排序。

    而对于自定义对象的排序,有两种实现方法:一是自定义一个比较器,即实现Comparator接口,不具有侵入性;二是让被排序对象实现Comparable接口,具有侵入性。

1 基本数据类型的数组排序

String[]  strs={"Asd","DC","dc","sad","SAD","asdf"};Arrays.sort(strs);System.out.println(Arrays.toString(strs));//第二个参数是comparatorArrays.sort(strs,Collections.reverseOrder());System.out.println(Arrays.toString(strs));//忽略大小写Arrays.sort(strs, String.CASE_INSENSITIVE_ORDER);System.out.println(Arrays.toString(strs));Integer[] nums={4,12,3,1};Arrays.sort(nums);System.out.println(Arrays.toString(nums));//第二个参数是comparatorArrays.sort(nums, Collections.reverseOrder());System.out.println(Arrays.toString(nums));
运行结果:
[Asd, DC, SAD, asdf, dc, sad][sad, dc, asdf, SAD, DC, Asd][Asd, asdf, dc, DC, sad, SAD][1, 3, 4, 12][12, 4, 3, 1]

2 基本数据类型的Collection排序

List<Integer> list=new ArrayList<Integer>();list.add(6);list.add(12);list.add(3);Collections.sort(list);System.out.println(list);//第二个参数是ComparatorCollections.sort(list, Collections.reverseOrder());System.out.println(list);

运行结果:
[3, 6, 12][12, 6, 3]

3 自定义对象的排序

3.1 使用comparator接口,自定义比较器

被比较的对象:
public class Coffee {private int price;private String brand;public Coffee(int price,String brand) {// TODO Auto-generated constructor stubthis.price=price;this.brand=brand;}public int getPrice() {return price;}public String getBrand() {return brand;}@Overridepublic String toString() {// TODO Auto-generated method stubreturn brand+"  "+price;}}
自定义比较器:
import java.util.Comparator;public class CoffeeComparator implements Comparator<Coffee> {@Overridepublic int compare(Coffee o1, Coffee o2) {// 先按照价格排序,从低到高,然后再按照名称排序,升序if (o1.getPrice()!=o2.getPrice()) {return o1.getPrice()-o2.getPrice();}else {return o1.getBrand().compareTo(o2.getBrand());}}}
测试代码:
这种方法其实使用了策略模式的思想,通过传入不同的比较器,来实现不同的排序。
List<Coffee> coffees=new ArrayList<Coffee>();Coffee mocha=new Coffee(12, "mocha");Coffee latte=new Coffee(10, "latte");Coffee capu=new Coffee(10, "copu");Coffee breve=new Coffee(15, "breve");coffees.add(breve);coffees.add(latte);coffees.add(mocha);coffees.add(capu);Collections.sort(coffees, new CoffeeComparator());System.out.println(coffees);
运行结果:
[copu  10, latte  10, mocha  12, breve  15]

3.2 实现comparable接口,具有一定侵入性

被比较对象:
public class Coffee implements Comparable<Coffee>{private int price;private String brand;public Coffee(int price,String brand) {// TODO Auto-generated constructor stubthis.price=price;this.brand=brand;}public int getPrice() {return price;}public String getBrand() {return brand;}@Overridepublic String toString() {// TODO Auto-generated method stubreturn brand+"  "+price;}@Overridepublic int compareTo(Coffee o) {// TODO Auto-generated method stubif (this.getPrice()!=o.getPrice()) {return o.getPrice()-this.getPrice();}else {return this.getBrand().compareTo(o.getBrand());}}}
测试代码:
List<Coffee> coffees=new ArrayList<Coffee>();Coffee mocha=new Coffee(12, "mocha");Coffee latte=new Coffee(10, "latte");Coffee capu=new Coffee(10, "copu");Coffee breve=new Coffee(15, "breve");coffees.add(breve);coffees.add(latte);coffees.add(mocha);coffees.add(capu);Collections.sort(coffees);System.out.println(coffees);
运行结果:
[breve  15, mocha  12, copu  10, latte  10]



0 0
原创粉丝点击