How do I check if a BigDecimal is in a Set or Map in a scale independent way?
问题 BigDecimal's equals() method compares scale too, so new BigDecimal("0.2").equals(new BigDecimal("0.20")) // false It's contested why it behaves like that. Now, suppose I have a Set<BigDecimal> , how do I check if 0.2 is in that Set, scale independent? Set<BigDecimal> set = new HashSet<>(); set.add(new BigDecimal("0.20")); ... if (set.contains(new BigDecimal("0.2")) { // Returns false, but should return true ... } 回答1: contains() will work as you want it to if you switch your HashSet to a