码迷,mamicode.com
首页 > 编程语言 > 详细

Java Compare

时间:2020-07-11 21:17:22      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:static   ide   ble   tor   name   rgs   --   http   attention   

技术图片


技术图片

Comparable

技术图片


技术图片

// Goods.java

import java.util.Arrays;

public class Goods implements Comparable {
    private String name;
    private double price;

    public Goods() {
    }

    public Goods(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "name=‘" + name + ‘\‘‘ +
                ", price=" + price +
                ‘}‘;
    }

    @Override
    public int compareTo(Object o) {
        if(o instanceof Goods){
            Goods goods = (Goods) o;
            if(this.price < goods.price) {
                return -1;
            }else if (this.price == goods.price){
                return 0;
            }else {
                return 1;
            }
        }
        throw new RuntimeException("Unacceptable Type");
    }

    public static void main(String[] args) {
//        Goods goods1 = new Goods("Nike", 50.0);
//        Goods goods2 = new Goods("Jordan", 56.0);
//        Goods goods3 = new Goods("Tok", 40.0);
//
//        System.out.println(goods1.compareTo(goods2));  // -1



        Goods[] arr = new Goods[3];
        arr[0] = new Goods("Nike", 50.0);
        arr[1] = new Goods("Jordan", 56.0);
        arr[2] = new Goods("Tok", 40.0);

        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr)); 
        // the output is:
        // [Goods{name=‘Tok‘, price=40.0}, Goods{name=‘Nike‘, price=50.0}, Goods{name=‘Jordan‘, price=56.0}]
    }
}

Comparator

技术图片

ATTENTION : Comparator 一般用于在Arrays.sort() 或 Collections.sort()中以匿名实现类的方式使用

 @Test
    public void test1(){
        String[] arrs = new String[]{"XZZ", "BB", "AA", "CC", "JJ", "DD", "EE"};
        Arrays.sort(arrs, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if(o1 instanceof String && o2 instanceof String){
                    String s1 = (String)o1;
                    String s2 = (String)o2;
                    return -s1.compareTo(s2); // there is a minus symbol, sort by down
                }
                throw new RuntimeException("Unacceptable Type");
            }
        });
        System.out.println(Arrays.toString(arrs)); // the output is : [XZZ, JJ, EE, DD, CC, BB, AA]
    }

Java Compare

标签:static   ide   ble   tor   name   rgs   --   http   attention   

原文地址:https://www.cnblogs.com/nedrain/p/13285391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!