Question

I am trying to write a code to remove certain objects from an instance of ArrayList.

Architecture:

  1. I have three classes - ClassA, ClassB and ClassC.
  2. ClassA is the parent class which has a list of ClassB objects.
  3. ClassB is parent of ClassC and has a list of ClassC objects.
  4. ClassC contains a private variable called "identifier" with associated getters/setters.
  5. ClassA has an API "getClassCList" to get a consolidated list of ClassC objects which are contained inside all the ClassB object which ClassA has.

Objective of the code:

  1. To prepare ClassA with instances of ClassB. Prepare ClassB with instances of ClassC.
  2. Invoke the API in ClassA and get a consolidated list of ClassC instances.
  3. Loop over the list returned in the Step 2 and find the ClassC instances which have a specific identifier. The objects matching would need to be removed from the list.

Issue with the code:

  1. When I call the .removeAll in the Class "Test.java" at line "classCList.removeAll(objectsTobeRemoved);" , the code actually removes the objects from the arraylist of ClassC instances which was returned by the API "getClassCList" of ClassA and not from the ClassCList present in the ClassB object. How I do I go about removing the ClassC instances from the ClassC list in ClassB?

  2. UPDATE: I ONLY have access to the instance of ClassA available with me.

  3. UPDATE 2: ClassA implements an interface called "IRetrieve". The only method available to get the ClassC instances from ClassA is through the "getClassCList" API. I also don't have a way to get the ClassB instances from ClassA.

Code:

 public interface IRetrieve {

        List<ClassC> getClassCList();

    }

    public class ClassA implements IRetrieve {

        private List<ClassB> classBList;

        public List<ClassB> getClassBList() {
            return classBList;
        }

        public void setClassBList(List<ClassB> classBList) {
            this.classBList = classBList;
        }

        public List<ClassC> getClassCList(){

            List<ClassC> classCList = new ArrayList<>();

            for(ClassB classB: classBList){
                classCList.addAll(classB.getClassCList());
            }

            return classCList;
        }
    }

    public class ClassB {

        private List<ClassC> classCList;

        public List<ClassC> getClassCList() {
            return classCList;
        }

        public void setClassCList(List<ClassC> classCList) {
            this.classCList = classCList;
        }
    }


    public class ClassC {

        private String identifier;

        public String getIdentifier() {
            return identifier;
        }

        public void setIdentifier(String identifier) {
            this.identifier = identifier;
        }
    }

    public class Test {
    public static void main(String[] args) {
        ClassA classA = new ClassA();
        classA.setClassBList(new ArrayList<ClassB>());
        for (int i = 0; i < 5; i++) {
            ClassB classB = new ClassB();
            classB.setClassCList(new ArrayList<ClassC>());
            for (int j = 0; j < 5; j++) {
                ClassC classC = new ClassC();
                classB.getClassCList().add(classC);
            }
            classA.getClassBList().add(classB);
        }

        removeClassCInstances(classA);
    }


    public static void removeClassCInstances(IRetrieve classA){

        //try removing a specific object from the ClassCList
        //using an instance of ClassA

        List<ClassC> classCList = classA.getClassCList();

        List<ClassC> objectsTobeRemoved = new ArrayList<>();

        for(ClassC classC : classCList){
            if(classC.getIdentifier().equals("1")){
                objectsTobeRemoved.add(classC);
            }
        }

        classCList.removeAll(objectsTobeRemoved);
    }
}
Was it helpful?

Solution

You can define API to remove objects in ClassA:

public class ClassA {
   ...
   public void removeAll(List<ClassC> objectsToRemove) {
       for(ClassB classB: classBList){
           classB.getClassCList().removeAll(objectsToRemove);
       }
   }
}

OTHER TIPS

classB.getClassCList().removeAll(objectsTobeRemoved);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top