A simple solution is to make a class "Edge" and save the start, end, and weight of the edge as follows:
public class Edge {
String start = "";
String end = "";
int weight = 0;
public Edge(String start, String end, int weight) {
this.start = start;
this.end = end;
this.weight = weight;
}
//check if the edge is the same with the intance
public boolean isSameEdge(Edge edge) {
if ((this.start.equals(edge.start) && this.end.equals(edge.end) && this.weight == edge.weight)) {
return true;
}
return this.start.equals(edge.end) && this.end.equals(edge.start) && this.weight == edge.weight;
}
@Override
public String toString() {
return "Edge{" +
"start='" + start + '\'' +
", end='" + end + '\'' +
", weight=" + weight +
'}';
}
}
And then at your main program create instances of edges and add them in an ArrayList but first check if they are already in the list.
ArrayList<Edge> edgeArrayList = new ArrayList<>();
Edge edge1 = new Edge("a", "b", 5);
Edge edge2 = new Edge("b", "a", 5);
boolean hasSame = false;
edgeArrayList.add(edge1);
for (Edge edge : edgeArrayList) {
if (edge.isSameEdge(edge2)) {
hasSame = true;
}
}
if (!hasSame) {
edgeArrayList.add(edge2);
}
System.out.println("List of edges: " + Arrays.toString(edgeArrayList.toArray()));
The output will be only the edge1 because edge1 and edge2 are the same.
List of edges: [Edge{start='a', end='b', weight=5}]