【问题标题】:Using the Comparable interface when comparing Strings比较字符串时使用 Comparable 接口
【发布时间】:2011-03-02 13:05:38
【问题描述】:

我搜索了这个问题,但我只发现一个线程有点令人困惑,所以我将在这里询问我希望能得到更清晰的答案。

我有一个任务是使用 Comparable 接口按客户名称对数组中的对象进行排序。到目前为止,我只用整数完成了这个,所以我不确定如何将字符串比较在一起。我该怎么办?这是我到目前为止的位置,假设我要使用 a.name 与 this.name 相比:

public int comparedTo(Customer a)
{

}   //end comparedTo

我还需要创建一个类来实现 Comparator 接口,以根据客户购买对值进行排序,我认为我这样做是正确的,但我想在弄错之前先确认一下。这是我为此所做的:

class NameComparator implements Comparator{
public int compare(Object cust1, Object cust2){    

    String cust1Purch = ((Customer)cust1).purchase;        
    String cust2Purch = ((Customer)cust2).purchase;

    return cust1Purch.compareTo(cust2Purch);
}

非常感谢任何帮助!

【问题讨论】:

    标签: java arrays interface comparator comparable


    【解决方案1】:

    没关系,但是您可以指定 Comparator 泛型类型,然后无需转换对象:

    class NameComparator implements Comparator<Customer>{
    public int compare(Customer cust1, Customer cust2){    
    
        String cust1Purch = cust1.purchase;        
        String cust2Purch = cust2.purchase;
    
        return cust1Purch.compareTo(cust2Purch);
    }
    

    【讨论】:

    • 那已经改变了!谢谢
    【解决方案2】:

    这是一个可能对您有所帮助的完整示例:

    一个CustomerComparator

    class CustomerComparator implements Comparator<Customer> {
    
        @Override
        public int compare(Customer c1, Customer c2) {
            return c1.name.compareTo(c2.name);   // or, simply c1.compareTo(c2);
        }
    }
    

    一个ComparableCustomer

    class Customer implements Comparable<Customer> {
    
        String name;
    
        public Customer(String name) {
            this.name = name;
        }
    
        @Override
        public int compareTo(Customer o) {
            return name.compareTo(o.name);
        }
    
        public String toString() {
            return name;
        }
    }
    

    一个简单的测试驱动程序:

    public class Test {
        public static void main(String[] args) {
    
            List<Customer> customers = Arrays.asList(new Customer("Bravo"),
                                                     new Customer("Charlie"),
                                                     new Customer("Delta"),
                                                     new Customer("Alpha"));
            Collections.sort(customers);
    
            // Or
            // Collections.sort(customers, new CustomerComparator());
    
            System.out.println(customers);
    
        }
    }
    

    (ideone.com demo)

    【讨论】:

    • 为什么会是 Collections.sort()?我的印象是排序是通过 Arrays.sort() 调用的。
    • Collections.sort 用于列表等。Arrays.sort() 用于数组。你真的可以使用任何一个。
    • 哦,好的。这听起来很熟悉。我可能应该意识到这一点,但我太专注于这项任务。感谢您的澄清。
    【解决方案3】:

    看起来不错。但是您可以使用泛型:

    class NameComparator implements Comparator<Customer> {
        public int compare(Customer cust1, Customer cust2) {..}
    }
    

    【讨论】:

    • 所以与其说是 String cust1Purch = ((Customer)cust1).purchase;,不如说 String cust1Purch = cust1.purchase; ?只是仔细检查。
    • @Lish - 是的。铸造将是多余的。
    • @Yanick Rochon 好吧,是的,但是程序员不需要强制转换。
    【解决方案4】:

    我似乎很适合Comparable 接口。那里没什么复杂的。

    至于Comparator,如果您不使用泛型,您还需要验证两个参数是否具有相同的基本类型,至少Comparable,因为您使用的是该接口:

    if (cust1 instanceof Comparable && cust2 instanceof Comparable) {
       Comparable c1 = (Comparable) cust1;
       Comparable c2 = (Comparable) cust2;
       return c1.compareTo(c2);
    } else {
       return false;
    }
    

    【讨论】:

    • 我的问题是我不确定如何比较 compareTo() 方法中的字符串。有什么见解吗?
    • 如果您查看String 类,它已经实现了Comparable 接口:)
    • 我看到有一次我刷新并且其他人说过。这个网站移动得很快! :)
    【解决方案5】:

    1) 我会使用泛型来定义你的比较器并避免额外的类转换:

    class NameComparator implements Comparator<Customer> {
        public int compare(Customer cust1, Customer cust2) {
          ...
        }
    }
    

    2) Java 中的 String 类已经实现了 Comparable 接口(@98​​7654321@)。因此,如果您只需要比较客户姓名或购买字符串,那么您可以将其委托给 String,这就是您已经在做的事情。

    【讨论】:

      猜你喜欢
      • 2018-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-15
      • 1970-01-01
      • 2012-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多