Here is my object constructor
static class Edge {
int source; // source node
int destination; // destination node
int weight; // weight of the edge
int predecessor; // previous node
public Edge() {};
public Edge(int s, int d, int w) { source = s; destination = d; weight = w; }
}
Now, here is the statement where I create a new Edge object
edges.add(new Edge(nodeStart, tempDest, tempWeight));
I need to skip over that statement if there has already been an object created with the same parameters (nodeStart, tempDest)
Basically, I don’t want to add the same edge twice.
edges.add(new Edge(0, 1, tempWeight));
edges.add(new Edge(0, 1, tempWeight));
If that happens, I want to make sure it only adds it once, and not new identical objects.
The proper way would be to make
edgesaSet<Edge>. And then properly override hashcode and equals.So, you should declare
edgeslike so:And you should implement hashCode and equals like so:
You should read up on properly implementing equals and hashCode. My examples are not great. But the general idea is conveyed.