Set.contains has a precise definition based upon equality:
More formally, returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e)).
It would violate the contract of the method of it used anything other than equality. And equality has a precise definition which says that it must be transitive (amongst other properties). An equality method which uses a tolerance is not transitive.
As such, there is no way for Set.contains to allow a tolerance.
However, this is not to say that you shouldn't ever check to see if a set contains a value within a tolerance of some value - just don't try to overload the concept of contains to do it.
For example, you could have a method which takes a NavigableSet (for example a TreeSet), and use its subSet method:
static boolean containsApprox(NavigableSet<Double> set, double target, double eps) {
return !set.subSet(target - eps, true, target + eps, true).isEmpty();
}
This just requests the portion of the set which runs from target-eps to target+eps (inclusive, as indicated by the true parameters). If this is non-empty, there is a value in the set within eps of the target.
This is clearly a separate concept from the standard Set.contains, so it is fine for this to do a contains check which doesn't share the same properties.
You can't do the same subSet trick with a HashMap because it is an unordered map - there is no efficient way to extract the values in a given range. You would have to iterate the entire set, as in Sun's answer, looking for a matching value.