2 回答
TA贡献1848条经验 获得超6个赞
所以,我做了这样的事情,我认为它可以满足你的要求。基本上,我所做的是以下内容。我将所有全局值添加到映射中,并在局部值的下一次迭代中检查 id 是否存在,然后更改isFavor.
public static void main(String[] args) throws ParseException {
List<Info> local = new ArrayList<>();
local.add(new Info(1,"msg1",false));
local.add(new Info(2,"msgTwo",false));
local.add(new Info(3,"msg3",true));
local.add(new Info(4,"msg4",true));
List<Info> global = new ArrayList<>();
global.add(new Info(1,"msg1",false));
global.add(new Info(2,"msg2",false));
global.add(new Info(3,"msg3",false));
global.add(new Info(4,"msg5",true));
Map<Integer, Info> map = new HashMap<>();
for (Info info : global) {
if (!map.containsKey(info.id)) {
map.put(info.id, info);
}
}
for (Info info : local) {
if (map.containsKey(info.id)){
map.get(info.id).isFavor = info.isFavor;
}
}
System.out.println(map.values());
}
TA贡献1865条经验 获得超7个赞
public Info [] merge(Info [] localArray, Info [] globalArray) {
List<Info> resultList = new ArrayList();
for(Info infoGlobal : globalArray) {
Info infoLocal = findInfo(infoGlobal.getId(), localArray);
if( infoLocal != null )
infoGlobal.setFavor(infoLocal.isFavor());
resultList.add(infoGlobal);
}
return resultList.toArray();
}
private Info findInfo(int id, Info [] infoArray) {
for(Info info : infoArray)
if(info.getId == id)
return info ;
return null ;
}
添加回答
举报