有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java比较方法违反了它的一般约定,如何使它具有可传递性?

我不明白它是如何不可传递的,请有人给我建议正确的方法。 如果两个值都是null

我返回0,另外两个语句非常明显

为什么我得到IllegalArgumentException 比较法违反其总合同

我的比较方法:(我比较双打)

@Override
public int compare(HashMap<String, String> lhs, HashMap<String, String> rhs) {

    double dist1 = 0;
    double dist2 = 0;

    int compInt1 = 0;
    int compInt2 = 0;

    if (lhs.get("dist") != null && rhs.get("dist") != null && !lhs.get("dist").equals("") && !rhs.get("dist").equals("")) {

        dist1 = Double.parseDouble(lhs.get("dist").substring(0, lhs.get("dist").length() - 3));
        dist2 = Double.parseDouble(rhs.get("dist").substring(0, rhs.get("dist").length() - 3));

        dist1 = dist1 * 100;
        dist2 = dist2 * 100;

        compInt1 = (int) dist1;
        compInt2 = (int) dist2;

    }

    if (compInt1 < compInt2) {
        return -1;
    } else if (compInt1 >= compInt2) {
        return 1;
    } else {
        return 0;
    }

}

共 (0) 个答案