Hi!
I try to create HashSet of the next object:
import java.nio.file.Path;
public class WatchedPath {
public Path dir;
public boolean recursive;
public Filter filter;
// Ctor with only Path - for comparing between the object (comparing is only by the path!)
public WatchedPath(Path dir) {
this.dir = dir;
}
// Ctor without filter
public WatchedPath(Path dir, boolean rec) {
this.dir = dir;
recursive = rec;
filter = null;
}
// Ctor with filter
public WatchedPath(Path dir, boolean rec, Filter filter) {
this.dir = dir;
recursive = rec;
this.filter = filter;
}
// hashCode function
public int hashCode () {
return dir.hashCode();
}
// override the equals method! comparing is only by the path!
public boolean equals(WatchedPath wp) {
return dir.equals(wp.dir);
}
// override compareTo
public int compareTo(WatchedPath wp) {
return dir.compareTo(wp.dir);
}
}
I thought the Hash's comparison between two object should be by their equals method, but for example, the next code will print nothing:
HashSet<WatchedPath> xx = new HashSet<WatchedPath>();
xx.add(wp); // wp is WatchedPath object.
if (xx.contains(new WatchedPath(wp.dir)))
System.out.println("EQUALS!!!");
why does it happens?
what should I do for makaing the code print the text?
(my original problem is in BiMap, but I guess it's gonna have the same solution)
Thanks,
Nate