我有两个不同的列表,一个是List<MainCategoriesAPI.Datum> datumList = new ArrayList<>();
,第二个是List<SubCategoryEnt> subCategoryEnts1 = new ArrayList<>();
。
我想要比较这两个列表,并获取datumList中不存在的ID。然后,我想从SubCategoryEnt中删除有关这些ID的数据。
答案 0 :(得分:2)
如果您定位到Android N以上
List<String> a1 = Arrays.asList("2009-05-18", "2009-05-19", "2009-05-21");
List<String> a2 = Arrays.asList("2009-05-18", "2009-05-18", "2009-05-19", "2009-05-19", "2009-05-20", "2009-05-21","2009-05-21", "2009-05-22");
List<String> result = a2.stream().filter(elem -> !a1.contains(elem)).collect(Collectors.toList());
或者您可以使用Collection接口的removeAll方法。
// Create a couple ArrayList objects and populate them
// with some delicious fruits.
Collection firstList = new ArrayList() {{
add("apple");
add("orange");
}};
Collection secondList = new ArrayList() {{
add("apple");
add("orange");
add("banana");
add("strawberry");
}};
// Show the "before" lists
System.out.println("First List: " + firstList);
System.out.println("Second List: " + secondList);
// Remove all elements in firstList from secondList
secondList.removeAll(firstList);
// Show the "after" list
System.out.println("Result: " + secondList);
答案 1 :(得分:1)
您必须使用for循环找到相似的ID,然后再次使用for循环从datumList中删除ID;
List<MainCategoriesAPI.Datum> datumList = new ArrayList<>();
List<SubCategoryEnt> subCategoryEnts1 = new ArrayList<>();
List<Integer> results = new ArrayList<Integer>();// this is for storing same ids
获取其他ID
// to get same ids
if (datumList.size() == subCategoryEnts1.size()) {
for (int i=0; i<datumList.size();i++){
int datIds = datumList.get(i);
for (int j=0; j<subCategoryEnts1.size();j++){
int subId = subCategoryEnts1.get(j);
if (datIds!=subId){
results.add(subId);
break;
}
}
}
}
删除ID
// to remove same id
for (int i=0; i<results.size();i++){
int datIds = results.get(i);
for (int j=0; j<datumList.size();j++){
int subId = datumList.get(j);
if (datIds==subId){
datumList.remove(j);
break;
}
}
}
希望这会对您有所帮助。
答案 2 :(得分:1)
检查以下内容以查找subCategoryEnts1
的缺失项
List missingIds = new ArrayList<SubCategoryEnt>();
for (SubCategoryEnt subCategory : subCategoryEnts1) {
for (MainCategoriesAPI.Datum datam : datumList) {
if (datam.id == subCategory.id){
missingIds.add(subCategory);
break;
}
}
}
现在从subCategoryEnts1
删除那些
subCategoryEnts1.removeAll(missingIds);