BigDecimal\'s equals()
method compares scale too, so
new BigDecimal(\"0.2\").equals(new BigDecimal(\"0.20\")) // false
It\'s c
HashSet#contains method can't serve your requirement, it implicitly call equals
method. You should iterate over Set
and use compareTo
method. If value is found than set a flag.
Set set = new HashSet<>();
set.add(new BigDecimal("0.20"));
boolean found=false;
for (BigDecimal bigDecimal : set) {
if(bigDecimal.compareTo(new BigDecimal("0.2"))==0){
System.out.println("Value is contain");
found=true;
break;
}
}
if(found)// Use this flag for codition.