2 回答

TA貢獻1848條經(jīng)驗 獲得超6個贊
所以,我做了這樣的事情,我認為它可以滿足你的要求?;旧希宜龅氖且韵聝?nèi)容。我將所有全局值添加到映射中,并在局部值的下一次迭代中檢查 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條經(jīng)驗 獲得超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 ;
}
添加回答
舉報