日期:2014-05-20  浏览次数:20734 次

map遍历查找缺少值的问题
今日遇到一个问题,需要整理每个分类下所缺少的尺码和颜色。
输入形式是一个
Execl表格
产品编码号颜色   属性值
产品编码号尺码   属性值
例如:
123颜色 蓝色,绿色
123尺码 M,L,LL
123颜色 黑色,绿色
123尺码 M,L
我现在已经整理成了Map的形式
    cid     颜色,尺码<属性值>
Map<String,<String,Set<String>>> map=
现在获取另一个map2,形式如下
    cid     颜色,尺码<属性值>
Map<String,<String,List<String>>> map=
要求遍历map1,查找出map2中没有的属性并放入到map3中。
求实现方法、
感觉逻辑太复杂了,弄混了

------解决方案--------------------

public class Test4 {
public static void main(String[] args) {
Map<String,Map<String,Set<String>>> oldmap=new HashMap<String, Map<String,Set<String>>>();



}
public Map<String,Map<String,Set<String>>> getothermap(Map<String,Map<String,Set<String>>> oldmap,Map<String,Map<String,Set<String>>> newmap){
Map<String,Map<String,Set<String>>> othermap=new HashMap<String, Map<String,Set<String>>>();

for(String cid:oldmap.keySet()){
Map<String,Set<String>> oldcidmap=oldmap.get(cid);
Map<String,Set<String>> newcidmap=newmap.get(cid);
Map<String,Set<String>> othercidmap=new HashMap<String, Set<String>>();
Set<String> oldcolorset=oldcidmap.get("color");
Set<String> oldsizeset=oldcidmap.get("size");
Set<String> newcolorset=newcidmap.get("color");
Set<String> newsizeset=newcidmap.get("size");
Set<String> othercolorset=new HashSet<String>();
Set<String> othersizeset=new HashSet<String>();
othercidmap.put("color", othercolorset);
othercidmap.put("size", othersizeset);

for(String attval:oldcolorset){
if(!newcolorset.contains(attval)){
othercolorset.add(attval);
}
}
for(String attval:oldsizeset){
if(!newsizeset.contains(attval)){
othersizeset.add(attval);
}
}
}
return othermap;

}
}

不知道你做哪个平台的,我是做京东的。
在我原有的代码基础上改了改,你试试把。

------解决方案--------------------
你为什么要把他弄成这个奇怪的一个数据结构。。
你把每种类型整合为一个类不是很方便么。

class Goods{
String id;
Set<String> colors;
Set<String> sizes;
//getter and setter
}