Map<List, List>的key value怎么对应

Map<List, List> parentidMap = new HashMap<List, List>();
List parentid = new ArrayList();
List listMaterials = new ArrayList();
for (WzMaterialsBigcategory wz : list) {
parentid.add(wz.getCategoryid());
listMaterials.add(wz);
}
parentidMap.put(parentid, listMaterials);

现在 key 和 value 没对应 想知道怎么可以对应起来

这样当然不行了,map中key是list的话相当于list的地址作为key,只能调出后面整个list的地址,无法与后一个list中的元素一一对应的,应该这样
Map<String, WzMaterialsBigcategory> map = new HashMap<String, WzMaterialsBigcategory>();

for(WzMaterialsBigcategory wz : list){
map.put(wz.getCategoryid, wz);

}
// 最后调用map.get(key) 就可以得到对应的对象了
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-06-06
对应了啊。parentid 对应的是 listMaterials追问

输出的结果好像不对应啊;结果是 {[1,2,3,4,5]=[aa,bb,cc,dd,ee]},结果应该就是这个???应该是{1=aa,2=bb,3=cc,4=dd,5=ee}才对吧?

追答

输出的结果是对应的 因为你Map 这样定义的所以 是 List 与List对应的。
{1=aa,2=bb,3=cc,4=dd,5=ee} 这样式String 和 Sting 对应

相似回答