关于强制转换成 Map 取值的问题。困扰了一天了,大家帮帮忙

Lucas- 2017-03-11 05:46:25
公司里别人写的代码片段,我拷出来部分代码 本地模拟代码如下: 在
String value = (String)attrMap.get(attrId); 为什么每次value都为空呢,找了半天没找到问题的所在,请大神不吝赐教。
Main方法用来模拟参数,testMap方法中取值的时候总是娶不到啊.........代码39行

public static void main(String[] args) throws Exception {

Map openParaMap = new HashMap();
Map resultMap = new HashMap();
List offerList = new ArrayList();
offerList.add("111001002364");
openParaMap.put("BILL_ID", "18811520894_NET");
openParaMap.put("OFFER_ID", offerList);
openParaMap.put("OP_TYPE", "CREATE");
openParaMap.put("CHANNEL_TYPE", "7");
openParaMap.put("EFF_DATE", null);
openParaMap.put("EXP_DATE", null);

Map offerAttrMaps = new HashMap();
Map offerAttr = new HashMap();

offerAttr.put("222222222", "18811520894");
offerAttr.put("333333333", "test_mac_001"); // mac地址
offerAttr.put("444444444", "197031001"); // 物料编码
offerAttr.put("666666666", "0");
offerAttrMaps.put("111001002364", offerAttr);
openParaMap.put("ATTRS", offerAttrMaps);
testMap(openParaMap);
}


public static void testMap(Map openParaMap){
ArrayList offerId = (ArrayList) openParaMap.get("OFFER_ID");
Object attrMapsObj = openParaMap.get("ATTRS");
Map attrMaps = (HashMap)attrMapsObj;
Iterator iter = (Iterator) attrMaps.keySet().iterator();
while (iter.hasNext()) {
String iterOffer = String.valueOf(iter.next());
Map attrMap = (HashMap) attrMaps.get(String.valueOf(iterOffer));
Iterator iterAttr = (Iterator) attrMap.keySet().iterator();
while(iterAttr.hasNext()){
//拼接属性
String attrId = String.valueOf(iterAttr.next());
String value = (String)attrMap.get(attrId);
System.out.println(attrId + ": " + value);
}
}
}
...全文
427 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
李德胜1995 2017-03-11
  • 打赏
  • 举报
回复
引用 5 楼 Mr__lu 的回复:
[quote=引用 4 楼 pany1209 的回复:] [quote=引用 3 楼 Mr__lu 的回复:] 打印的结果:

222222222: null
444444444: null
666666666: null
333333333: null


String attrId = String.valueOf(iterAttr.next());
  long attrIdNew= Long.parseLong(attrId);
把AttrId再转为long类型就可以了[/quote] 这样可以达到效果,能具体解释下原因吗,强制转换成HashMap后,里面存的还是Long类型吗,不是String的吗[/quote] offerAttrMaps一开始就是HashMap,向上转型,向下转型,转来转去都一个样。。。为啥会变String类型?
Lucas- 2017-03-11
  • 打赏
  • 举报
回复
引用 4 楼 pany1209 的回复:
[quote=引用 3 楼 Mr__lu 的回复:] 打印的结果:

222222222: null
444444444: null
666666666: null
333333333: null


String attrId = String.valueOf(iterAttr.next());
  long attrIdNew= Long.parseLong(attrId);
把AttrId再转为long类型就可以了[/quote] 这样可以达到效果,能具体解释下原因吗,强制转换成HashMap后,里面存的还是Long类型吗,不是String的吗
李德胜1995 2017-03-11
  • 打赏
  • 举报
回复
引用 3 楼 Mr__lu 的回复:
打印的结果:

222222222: null
444444444: null
666666666: null
333333333: null


String attrId = String.valueOf(iterAttr.next());
  long attrIdNew= Long.parseLong(attrId);
把AttrId再转为long类型就可以了
Lucas- 2017-03-11
  • 打赏
  • 举报
回复
打印的结果:

222222222: null
444444444: null
666666666: null
333333333: null

Lucas- 2017-03-11
  • 打赏
  • 举报
回复
不对,这样是有值的。 源代码里put进去的类型为Long,不是String
[code=java]
        offerAttr.put("222222222", "18811520894");
        offerAttr.put("333333333", "test_mac_001"); // mac地址
        offerAttr.put("444444444", "197031001"); // 物料编码
        offerAttr.put("666666666", "0"); 
        offerAttrMaps.put("111001002364", offerAttr);
        openParaMap.put("ATTRS", offerAttrMaps);
        testMap(openParaMap);
[/code] 改为
[code=java]
 offerAttr.put(222222222L, "18811520894");
	        offerAttr.put(333333333L, "test_mac_001"); // mac地址
	        offerAttr.put(444444444L, "197031001"); // 物料编码
	        offerAttr.put(666666666L, "0"); 
	        offerAttrMaps.put("111001002364", offerAttr);
	        openParaMap.put("ATTRS", offerAttrMaps);
	        testMap(openParaMap);
	}
[/code]
李德胜1995 2017-03-11
  • 打赏
  • 举报
回复
你打印结果是啥???

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧