比较两个list,返回多余部分和删除部分
1 import java.util.ArrayList; 2 import java.util.List; 3 4 public class CompareTwoListUtil { 5 public static void getList(List<Long> oldGroupIdList, List<Long> newGroupIdList, List<Long> addList, List<Long> delList) { 6 if(newGroupIdList != null && !newGroupIdList.isEmpty()) { 7 //以aList为准,aList比bList多余的部分添加,少的部分删除,相同的部分更新 8 List<Long> updateList1 = new ArrayList(); 9 List<Long> updateList2 = new ArrayList(); 10 11 //找出需要添加的部分和共同的部分 12 for (int i = 0; i < newGroupIdList.size(); i++) { 13 boolean isAdd = true; 14 Long newGroupId = newGroupIdList.get(i); 15 for (int j = 0; j < oldGroupIdList.size(); j++) { 16 Long bTemp = oldGroupIdList.get(j); 17 if (newGroupId.equals(bTemp)) { 18 updateList1.add(newGroupId); 19 isAdd = false; 20 } 21 } 22 if (isAdd) { 23 addList.add(newGroupId); 24 } 25 } 26 //找出需要删除的部分和共同的部分 27 for (int i = 0; i < oldGroupIdList.size(); i++) { 28 boolean isDel = true; 29 Long bTemp = oldGroupIdList.get(i); 30 for (int j = 0; j < newGroupIdList.size(); j++) { 31 Long aTemp = newGroupIdList.get(j); 32 if (aTemp.equals(bTemp)) { 33 updateList2.add(aTemp); 34 isDel = false; 35 } 36 } 37 if (isDel) { 38 delList.add(bTemp); 39 } 40 } 41 }else { 42 if(oldGroupIdList != null && !oldGroupIdList.isEmpty()) { 43 for (Long aLong : oldGroupIdList) { 44 delList.add(aLong); 45 } 46 } 47 } 48 } 49 }