简体   繁体   中英

ArrayList .contains() sometimes true, sometimes false

I´m writing a simple Program which simulates a graph. This is how i implement a vertex: ( i used the word nodes for neighbours, thats a little confusing maybe..)

public class Vertex {

private String name;
private int nodes;

public Vertex(String name) {
    this.name = name;
    nodes = 0;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Vertex other = (Vertex) obj;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equalsIgnoreCase(other.name))
        return false;
    return true;
}

In my Graph class I wrote a method which returns the neighbours(nodes) of a specific vertex:

public List<Vertex> getNodesOf(Vertex v) {
    List<Vertex> nodes = new ArrayList<>();
    if (vertices.contains(v)) {              //vertices is an ArrayList<Vertex>
        // adds all neighbours to nodes...
        return nodes;
    } else {
        Terminal.printLine("Error, " + v.getName() + " does not exist here!");
        return nodes;

When I call that method from my main method, it works fine:

List<Vertex> nodes = g.getNodesOf(new Vertex(input[1]));    //input[1] is a name typed by the user
        if (nodes != null) {
            for (Vertex node : nodes) {
                System.out.println(node.getName());
            }
        }

But I have another class for the dijkstra-algorithm to find the shortest path. this algorithm also needs the neighbours. this is a part of the code:

    Vertex nearest = null;
    int distanceInt = 9999;
    for (Vertex vertex : unvisited) {
        if (distance.containsKey(vertex)) {
            if (distance.get(vertex) <= distanceInt) {
                nearest = vertex;
                distanceInt = distance.get(vertex);
            }
        }
    }

    if (graph.getNodesOf(nearest).contains(vertex)) {
        // do something...
    }

But when i call the method from here, it always says that the ArrayList doesn´t contain the Vertex and the //do something... will never be reached.

I overrided the equals and hashcode method with eclipse, so i thought, this was not the problem.

What´s my mistake?

Your equals()-hashCode()-implementation is broken. The spec says that equal objects must have equal hash-codes. But in your equals()-method you ignore the case of names while the hash-method does not ignore it.

This behaviour is relevant if you use hash-based maps, and distance.containsKey(vertex) looks like a typical map-lookup so I assume that your distance -object is a kind of Map .

Solution: Make your hashCode() -method also case-insensitive, or make your equals() -method case-sensitive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM