I'm trying to make an ArrayList of objects Edge, without any duplicates :
public class Edge implements Comparable<Edge>{
private int vertex1, vertex2;
//constructor, getter ...
@Override
public int compareTo(Edge o) {
if (vertex1 == o.getVertex1() && vertex2 == o.getVertex2()) return 0;
return 1;
}
}
I thought that using a HashSet who doesn't accept duplicates will work but I was wrong :
ArrayList<Edge> edges = new ArrayList<Edge>();
//fill list (with possible duplicates)
//delete duplicates
Set<Edge> hs = new HashSet<Edge>();
hs.addAll(edges);
edges.clear();
edges.addAll(hs);
This method works with ArrayList of String, Integer, but I don't understand why it doesn't works for this case.
Sorry for my english, I'm french.