What are classes which `a.compareTo(b) == 0` does NOT mean that `a.equals(b) == true`?

I know that BigDecimal has that property, is there any other like that?

Compares this BigDecimal with the specified Object for equality. Unlike compareTo, this method considers two BigDecimal objects equal only if they are equal in value and scale (thus 2.0 is not equal to 2.00 when compared by this method).

in other words, new BigDecimal("2.0").compareTo(new BigDecimal(2)) == 0 is true, but new BigDecimal("2.0").equals(new BigDecimal(2)) is false.

Go ahead and test this code:

public  BlaBla { static BigDecimal first = BigDecimal.ONE; static BigDecimal second = new BigDecimal("1.0"); static BigDecimal third = new BigDecimal("1.00"); static BigDecimal fourth = BigDecimal.TEN.divide(BigDecimal.TEN); static BigDecimal fifth = BigDecimal.TEN.divide(new BigDecimal("10.0000000000000000000")); public static void main(String ... strgs) { Set<BigDecimal> treeSet = new TreeSet<>(); treeSet.add(first); treeSet.add(second); treeSet.add(third); treeSet.add(fourth); treeSet.add(fifth); Set<BigDecimal> hashSet = new HashSet<>(); hashSet.add(first); hashSet.add(second); hashSet.add(third); hashSet.add(fourth); hashSet.add(fifth); System.out.println(treeSet.size() + ":" + hashSet.size()); } } 

submitted by /u/akerro

You might also like
Leave A Reply

Your email address will not be published.