简体   繁体   English

如何从Java中的TreeMap中提取BigDecimals?

[英]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. 我在BigDecimal中有一个同时包含两个值的TreeMap,我试图打印一些特定值,以便以后可以执行更复杂的操作,但是我不知道BigDecimals使用哪种Map方法,因为这些方法需要int值。 This is my code : 这是我的代码:

Map methods expect Object or BigDecimal (depends on method). 映射方法期望使用Object或BigDecimal(取决于方法)。 But int primitive type doesn't autoboxed to BigDecimal. 但是int原语类型不会自动装箱到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());
        }
    }

}

Here is an example showing you how you can use a BigDecimal as a key for a map. 这是一个示例,向您展示如何使用BigDecimal作为地图的键。 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM