Java——類比較子

來源:互聯網
上載者:User

標籤:

1、Product類

 1 public class Product 2 { 3     private int pid; 4     private String name; 5     private double price; 6  7     public Product() 8     { 9         // TODO Auto-generated constructor stub10     }11 12     public Product(int pid, String name, double price)13     {14         this.pid = pid;15         this.name = name;16         this.price = price;17     }18 19     public int getPid()20     {21         return pid;22     }23 24     public void setPid(int pid)25     {26         this.pid = pid;27     }28 29     public String getName()30     {31         return name;32     }33 34     public void setName(String name)35     {36         this.name = name;37     }38 39     public double getPrice()40     {41         return price;42     }43 44     public void setPrice(double price)45     {46         this.price = price;47     }48 49 }

2、類比較子:重寫compare方法

 1 public class MyCompare implements Comparator<Product> // 泛型<Product>表示該比較子只能比較Product類型 2 { 3  4     @Override 5     public int compare(Product p1, Product p2) 6     { 7         if (p1.getPrice() < p2.getPrice()) 8         { 9             return -1;10         }11         else if (p1.getPrice() > p2.getPrice())12         {13             return 1;14         }15         else16         // 如果價格一樣,按照id排序17         {18             if (p1.getPid() < p2.getPid())19             {20                 return -1;21             }22             else if (p1.getPid() > p2.getPid())23             {24                 return 1;25             }26             else27             {28                 return 0;29             }30         }31     }32 33 }

3、在ArrayList中使用比較子

 1 public class ProductSort 2 { 3     public static void main(String[] args) 4     { 5      6                 Product p1 = new Product(1, "爆米花手機", 1000000.00); 7         Product p2 = new Product(2, "iPhone 7s", 5088.00); 8         Product p3 = new Product(3, "小米5s", 1999.99); 9         Product p4 = new Product(4, "NIIT課程", 12800.00);10         Product p5 = new Product(5, "百達翡麗", 1980000.00);11         Product p6 = new Product(6, "寶瑪electric", 1999.99);12         Product p7 = new Product(7, "TIIN課程", 12800.00);13                 // 獲得數組準備排序14         Product[] proArr = { p1, p2, p3, p4, p5, p6, p7 };15 16         Arrays.sort(proArr, new MyCompare());17 18         for (Product p : proArr)19         {20             System.out.println(p.getPid() + " " + p.getName() + " "21                     + p.getPrice());22         }23     }24 }    

 

Java——類比較子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.