Repository navigation
Conversation
Test Results286 tests +4 283 ✅ +4 6s ⏱️ ±0s Results for commit 84a674a. ± Comparison against base commit b42879b. This pull request skips 1 and un-skips 1 tests. |
mperor
marked this pull request as ready for review
October 28, 2026 07:16
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What you'll learn
BigDecimal.equals()compares both unscaled value and scale, so"2.0"and"2.00"are not equalBigDecimal.compareTo()only compares numeric value, so it treats them as equalHashSet(usesequals/hashCode) keeps both, whileTreeSet(usescompareTo) keeps only onenew BigDecimal(double)exposes the exact binary floating-point value, whileBigDecimal.valueOf(double)goes throughDouble.toStringfor the expected decimal;stripTrailingZeros()normalizes scale soequalsagrees withcompareToWhy it matters
Mixing
equals/hashCodewithcompareTo-based collections is a classic source of subtle bugs — deduplication that silently "half-works" depending on whichSetimplementation you pick, orBigDecimalconstants that look equal but aren't.Read more
BigDecimal#equals Javadoc
Quiz
Given
Set<BigDecimal> set = new TreeSet<>(); set.add(new BigDecimal("2.0")); set.add(new BigDecimal("2.00"));, what isset.size()?Answer
1 — `TreeSet` orders and deduplicates using `compareTo`, which ignores scale, so the second `add` is treated as a duplicate and discarded. A `HashSet` in the same scenario would keep both (size 2), since it uses `equals`/`hashCode`, which do consider scale.Note on local verification: this environment only had JDK 21 installed, and the Gradle build requires JDK 25 via toolchain; JDK 25 auto-provisioning was blocked by network policy here, so I couldn't run
./gradlewlocally. I compiled and ran the test manually against JDK 21 beforehand to check the logic, and CI (JDK 25) has now confirmed all tests pass.Closes #92