带键值的哈希映射 - 如何使用bigdecimal键获取值?

时间:2014-07-04 04:59:31

标签: java oracle

使用键值的哈希映射。如何使用bigdecimal键获取值?

month_id.intValue()。这不起作用

public String getMonthNameById(BigDecimal month_id){

    String monthName = "";

        Map<Integer, String> months = new HashMap<Integer, String>();

        months.put(1, "January");

        months.put(2, "February");

        months.put(3, "March");

        months.put(4, "April");

        months.put(5, "May");

        months.put(6, "June");

        months.put(7, "July");

        months.put(8, "August");

        months.put(9, "September");

        months.put(10, "October");


        months.put(11, "November");

        months.put(12, "December");


        monthName = (String)months.get(month_id.intValue());

        return monthName;
}

1 个答案:

答案 0 :(得分:0)

如果我能猜出你可能在做什么,我想出了这个并且它正在发挥作用。请看一下:

import java.math.BigDecimal;
import java.util.*;
public class MyTest {
    public static void main(String[] args) {
        BigDecimal myBigDecimal = new BigDecimal(11);
        Map<Integer, String> myMap = new HashMap<Integer, String>();
        myMap.put(new Integer(11), "Hello World!");
        String message = (String) myMap.get(myBigDecimal.intValue());
        System.out.println(message);
    }
}

<强>输出:

C:\Mine\JAVA\J2SE\classes>java MyTest
Hello World!