如何在Java中比较多个类? [英] how to compare on multiple classes in java?

查看:86
本文介绍了如何在Java中比较多个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我已经在比较器上进行了编写,该排序器对整数和字符串数组进行了排序。从代码中可以看到,如果两个类不同,则String类的值大于值。但是,这仅允许两个类。如果我想向数组添加另一个基本类型,例如Float,该怎么办?我必须在if-else语句中添加更多代码。有没有一种方法可以执行比较而不必为我要比较的每个其他类添加语句?

  import java.util .arrays; 
import java.util.Comparator;

公共类SampleComparator实现Comparator< Object> {

public static void main(String [] args){
Object [] inputData = {new String( pizza),new Integer(0),
new String (苹果),新的Integer(5),新的String( pizza),
新的Integer(3),新的Integer(7),新的Integer(5)};
Arrays.sort(inputData,新的SampleComparator());
System.out.println(Arrays.asList(inputData));
}

public int compare(Object o1,Object o2){
if(o1.getClass()。equals(o2.getClass())){
返回((Comparable)o1).compareTo((Comparable)o2);
} else {
if(o1.getClass()。getCanonicalName()。equals( java.lang.String)){
返回1;
} else {
返回-1;
}
}

}

}

输出:

  [0、3、5、5、7,苹果,比萨饼,比萨饼] 


解决方案


我有向if-else语句添加更多代码。有没有一种方法可以实现比较而不必为我要比较的每个其他类添加语句?


您可能想比较



所以可以比较它们在本机上的比较器,然后对它们进行排序(例如,所有整数在所有浮点数之前在所有字符串之前)。



  public int compare(Object o1,Object o2){
//也许这里有一些空检查?

if(o1.getClass()。equals(o2.getClass())){
//如果它们不可比较怎么办?
return((Comparable)o1).compareTo((Comparable)o2);
}否则{
//例如按类名按字母顺序比较
// //另一个想法是包含所有受支持类的映射,
//向他们分配订单

返回o1.getClass()。getName()。compareTo(o2.getClass()。getName());
}

}

很难上来与未知类的有意义的比较。您可能需要组成一个支持的类和显式规则的列表,您希望它们如何进行比较。


Right now, I have written at comparator that sorts an array of Integers and Strings. As you can see from the code, if the two classes aren't the same, then the String class takes the greater than value. However, this only allows for two classes. What if I want to add another primitive type to my array, such as Float? I'd have to add more code to to if-else statement. Is there a way to implement compare without having to add a statement for each additional class I want to compare?

import java.util.Arrays;
import java.util.Comparator;

public class SampleComparator implements Comparator<Object> {

public static void main(String[] args) {
    Object[] inputData = { new String("pizza"), new Integer(0),
            new String("apples"), new Integer(5), new String("pizza"),
            new Integer(3), new Integer(7), new Integer(5) };
    Arrays.sort(inputData, new SampleComparator());
    System.out.println(Arrays.asList(inputData));
}

public int compare(Object o1, Object o2) {
    if (o1.getClass().equals(o2.getClass())) {
        return ((Comparable)o1).compareTo((Comparable)o2);
    } else {
        if(o1.getClass().getCanonicalName().equals("java.lang.String")){
            return 1;
        } else {
            return -1;
        }
    }

}

 }

output:

[0, 3, 5, 5, 7, apples, pizza, pizza]

解决方案

I'd have to add more code to to if-else statement. Is there a way to implement compare without having to add a statement for each additional class I want to compare?

You probably want to compare objects of the same class using their native comparators, and then have some order on the classes (e.g.. all ints before all floats before all strings).

So you could compare the classes first, and then if equal compare the objects.

public int compare(Object o1, Object o2) {
    // maybe some null checks here?

    if (o1.getClass().equals(o2.getClass())) {
        // and what if they are not Comparable ?
        return ((Comparable)o1).compareTo((Comparable)o2);
    } else {
        // for example compare by class name alphabetically
        // another idea would be a map with all supported classes,
        // assigning them an order

        return o1.getClass().getName().compareTo(o2.getClass().getName());
    }

}

It will be difficult to come up with a meaningful comparison of unknown classes. You probably need to make up a list of supported classes and explicit rules how you want them compared.

这篇关于如何在Java中比较多个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆