Search code examples
javabigdecimal

How do i extract BigDecimals from a TreeMap in Java?


I have a TreeMap with both values in BigDecimal, i'm trying to print some specific values so later i can do more complex operations, but i don't know which Map methods to use for BigDecimals, as the methods expect int values. This is my code :


Solution

  • Map methods expect Object or BigDecimal (depends on method). But int primitive type doesn't autoboxed to BigDecimal. You could create instance manually.

    public class DemoApplication {
    
        public static void main(String[]args) {
            Map<BigDecimal, BigDecimal> map = new HashMap<>();
            // ...
    
            if (map.containsKey(BigDecimal.valueOf(400))) {
                System.out.println(map.keySet());
            }
        }
    
    }