0

I have two different lists, one is List<MainCategoriesAPI.Datum> datumList = new ArrayList<>(); and second is List<SubCategoryEnt> subCategoryEnts1 = new ArrayList<>();.

What I want is to compare these two lists and get those ids which are not present in datumList. And then, I want to delete the data regarding these ids from SubCategoryEnt.

intellignt_idiot
  • 1,962
  • 2
  • 16
  • 23
Nabeel Ahmed
  • 223
  • 5
  • 15
  • Loop through those list and compare ids to get desired result – Md. Asaduzzaman Dec 26 '19 at 07:14
  • use `android.support.v7.util.DiffUtil` - the docs say: *"DiffUtil is a utility class that can calculate the difference between two lists and output a list of update operations that converts the first list into the second one."* – pskink Dec 26 '19 at 07:26

2 Answers2

2

Check below to find missing items of subCategoryEnts1

List missingIds = new ArrayList<SubCategoryEnt>();

for (SubCategoryEnt subCategory : subCategoryEnts1) {
    for (MainCategoriesAPI.Datum datam : datumList) {
        if (datam.id == subCategory.id){
            missingIds.add(subCategory);
            break;
        }
    }
}

Now remove those from subCategoryEnts1

subCategoryEnts1.removeAll(missingIds);
1

You have to use for loop to find the similar ids and the again use for loop to remove ids from datumList;

List<MainCategoriesAPI.Datum> datumList = new ArrayList<>();
List<SubCategoryEnt> subCategoryEnts1 = new ArrayList<>();
List<Integer> results = new ArrayList<Integer>();// this is for storing same ids

To get different ids

// 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;
                }
            }
        }
    }

to remove ids

// 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;
                }
            }
        }

Hope this will help you.

Sanwal Singh
  • 1,765
  • 3
  • 17
  • 35