类Collections与接口Comparator

Posted stickitout

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了类Collections与接口Comparator相关的知识,希望对你有一定的参考价值。

类Collections与接口Comparator的简单使用

 

  1 import java.util.ArrayList;
  2 import java.util.Collections;
  3 import java.util.Comparator;
  4 import java.util.List;
  5 
  6 public class TestCompare {
  7 
  8     public static void main(String[] args) {
  9 
 10         List<BookInfo> list = new ArrayList<BookInfo>();
 11         list.add(new BookInfo(1, "aa", "hskahs", 10));
 12         list.add(new BookInfo(14, "sw", "zxs", 40));
 13         list.add(new BookInfo(31, "as", "zx", 10));
 14         list.add(new BookInfo(24, "ds", "cds", 20));
 15         list.add(new BookInfo(5, "rfr", "cvb", 17));
 16         list.add(new BookInfo(20, "saa", "cdg", 100));
 17         list.add(new BookInfo(19, "cd", "def", 80));
 18 
 19         System.out.println("----------ID排序----------");
 20         Collections.sort(list, new BookInfoIDCompare());
 21         for (BookInfo bookInfo : list) {
 22             System.out.println(bookInfo);
 23         }
 24         
 25         System.out.println("----------名称排序----------");
 26         
 27 
 28         Collections.sort(list, new BookInfoNameCompare());
 29         for (BookInfo bookInfo : list) {
 30             System.out.println(bookInfo);
 31         }
 32 
 33         System.out.println("----------作者排序----------");
 34         
 35 
 36         Collections.sort(list, new BookInfoAuthorCompare());
 37         for (BookInfo bookInfo : list) {
 38             System.out.println(bookInfo);
 39         }
 40         
 41         
 42         System.out.println("-------价格降序排序-------------");
 43         
 44 
 45         Collections.sort(list, new BookInfoPriceCompare());
 46         for (BookInfo bookInfo : list) {
 47             System.out.println(bookInfo);
 48         }
 49     }
 50 }
 51 
 52 class BookInfoIDCompare implements Comparator<BookInfo> {
 53 
 54     @Override
 55     public int compare(BookInfo o1, BookInfo o2) {
 56 
 57         return o1.getId() > o2.getId() ? 1 : (o1.getId() < o2.getId() ? -1 : 0);
 58     }
 59 
 60 }
 61 
 62 class BookInfoNameCompare implements Comparator<BookInfo> {
 63 
 64     @Override
 65     public int compare(BookInfo o1, BookInfo o2) {
 66 
 67         return o1.getName().compareTo(o2.getName());
 68     }
 69 }
 70 
 71 class BookInfoAuthorCompare implements Comparator<BookInfo> {
 72 
 73     @Override
 74     public int compare(BookInfo o1, BookInfo o2) {
 75 
 76         return o1.getAuthor().compareTo(o2.getAuthor());
 77     }
 78 }
 79 
 80 class BookInfoPriceCompare implements Comparator<BookInfo> {
 81 
 82     @Override
 83     public int compare(BookInfo o1, BookInfo o2) {
 84         return o1.getPrice() > o2.getPrice() ? -1 : (o1.getPrice() < o2.getPrice() ? 1 : 0);
 85     }
 86 }
 87 
 88 class BookInfo {
 89     private int id;
 90     private String name;
 91     private String author;
 92     private int price;
 93 
 94     public BookInfo(int id, String name, String author, int price) {
 95         super();
 96         this.id = id;
 97         this.name = name;
 98         this.author = author;
 99         this.price = price;
100     }
101 
102     public BookInfo() {
103         super();
104     }
105 
106     public int getId() {
107         return id;
108     }
109 
110     public void setId(int id) {
111         this.id = id;
112     }
113 
114     public String getName() {
115         return name;
116     }
117 
118     public void setName(String name) {
119         this.name = name;
120     }
121 
122     public String getAuthor() {
123         return author;
124     }
125 
126     public void setAuthor(String author) {
127         this.author = author;
128     }
129 
130     public int getPrice() {
131         return price;
132     }
133 
134     public void setPrice(int price) {
135         this.price = price;
136     }
137 
138     @Override
139     public String toString() {
140         return "BookInfo [id=" + id + ", name=" + name + ", author=" + author + ", price=" + price + "]";
141     }
142 
143 }

 

 

以上是关于类Collections与接口Comparator的主要内容,如果未能解决你的问题,请参考以下文章

java Comparable与Comparator接口辨析

Comparator与Comparable 有什么不同?

comparator接口与comparable接口的区别

关于Comparable与Comparator

Comparable与Comparator的区别

Comparable接口和Comparator接口的不同用法