使用键值的哈希映射。如何使用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;
}
答案 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!