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 :
Asked
Active
Viewed 420 times
0
-
Related: [Why Map.containsKey() takes an Object parameter instead of a speciallized type? [duplicate\]](https://stackoverflow.com/questions/8434153/why-map-containskey-takes-an-object-parameter-instead-of-a-speciallized-type) – Ole V.V. Feb 26 '19 at 14:36
-
2It's not clear what you want to accomplish. The map does not contain the key 400 because that's an integer. It would be `map.containsKey(new BigDecimal(400))` – Michael Feb 26 '19 at 14:36
-
*as the methods expect int values* No they don’t. There’s no problem giving them a `BigInteger`. – Ole V.V. Feb 26 '19 at 14:37
2 Answers
1
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());
}
}
}

ilinykhma
- 980
- 1
- 8
- 14
-
This prints `[400]`. Which just *might* explain the asker’s confusion. `400` *looks* like an `int`, but it really is a `BigInteger`. Or more precisely, the result of `toString` having been called implicitly on a `BigInteger`. – Ole V.V. Feb 26 '19 at 14:41
-
I can't use .put as the Map already has the keys and values set (as BigDecimals) and i cannot change them. I've tried just using BigDecimal.valueOf on containsKey but i get the following exception : java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Integer – R.S. Feb 26 '19 at 14:46
-
1@ole-v-v The question is not clear. I think the problem was that the output of the author code is empty, but may be you are right. – ilinykhma Feb 26 '19 at 14:48
-
If you get ClassCastException, i think the value returned from getRowDTO.getMapSaldi() is not Map
. Can you show listing or at least declaration of getMapSaldi()? – ilinykhma Feb 26 '19 at 14:52 -
Ok, this worked. The problem was the keySet method, i mistakenly thought i could use it to get the value, but i used map.get(BigDecimal.valueOf(400)) instead and it works as intended. Thank you. – R.S. Feb 26 '19 at 14:57
0
Here is an example showing you how you can use a BigDecimal as a key for a map. I will be using a hashmap for this example.
Map<BigDecimal. BigDecimal> map = new HashMap<>();
//assuming this map has some values in it.
if(map.containsKey(new BigDecimal(400)){
//do something
}

Bas van der Linden
- 389
- 1
- 10
-
The map is already parametrized by the method tho', so it already contains keys and values of BigDecimal type. Wouldn't this just create a new BigDecimal that doesn't contain the value i need? – R.S. Feb 26 '19 at 14:50
-
@LordGriffith i just created the HashMap for demonstration perpouses, for you it would ofcouse be the map that you are using. – Bas van der Linden Feb 26 '19 at 14:53