how to compare on multiple classes in java?

感情迁移 提交于 2019-12-23 17:31:10

问题


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]

回答1:


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.




回答2:


If you just want to make sure you group classes together, you could do something like

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



回答3:


You can always compare the toString() representation:

public int compare(Object o1, Object o2) {
    return o1.toString().compareTo(o2.toString());
}


来源:https://stackoverflow.com/questions/7092450/how-to-compare-on-multiple-classes-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!