English 中文(简体)
Java: .equals() failing for sets (JGraphT)
原标题:

I can t figure out what s going wrong here. This test fails:

@Test
    public void testSimpleCase() {
        assertTrue(JGraphtUtilities.graphEquality(ChooseRootTest.generateSimpleCaseGraph(), ChooseRootTest.generateSimpleCaseGraph()));
    }

public static <V, E> boolean graphEquality(Graph<V, E> g0, Graph<V, E> g1) {

    boolean result = true;

    if (g0.edgeSet().equals(g1.edgeSet()) && g0.vertexSet().equals(g1.vertexSet())) {
        for (E e : g0.edgeSet()) {
            if (g0.getEdgeWeight(e) != g1.getEdgeWeight(e)) {
                result = false;
            }
        }
    }
    else {
        return false; //for the above test, this is what is returned
    }

    return result;
}

The debugger shows that the method decides that the two vertex sets and edge sets aren t equal, so it returns false. How is this possible?

Side note: I m trying to write an equality check for JGraphT graphs. How is it possible that this hasn t been done already?

UPDATE: I think that DefaultWeightedEdge doesn t override equals, so that wouldn t work. I did a different way of checking that edges exist between all necessary vertices, and now it seems to work.

最佳回答

According to the JavaDoc DefaultWeightedEdge hasn t implemented equals() and hashCode() and thus uses the methods defined in java.lang.Object. This means that two DefaultWeightedEdge objects a and b with the same values will not return true from a.equals(b). That would only return true if a and b actually refer to the same object.

You need to use an edge implementation class that implements .equals() and hashCode() to get useful results here.

问题回答

I m not familiar with JGraphT, but there are two issues I can think of.

First, what does it mean for two sets of edges to be equivalent? What is it that makes two edges equivalent? If I create a graph, and create an identical graph separately, both might have the same structure. But if edge comparison of two matching edges uses the node identities, the two edges would not be "equal".

Second, I ve noted this in the JavaDocs:

"The graph implementation may maintain a particular set ordering (e.g. via LinkedHashSet) for deterministic iteration, but this is not required. It is the responsibility of callers who rely on this behavior to only use graph implementations which support it."

I would try (at least for sanity) to make sure that the two sets contain each other, as it is possible that equals is not implemented correctly (e.g., it may take order into account).





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签