I have a question regarding validating data in a nested for loop.
public class Object1{
private String obj1Name;
private String obj1Desc;
private List<Object2> object2List;
//Setters and getters
}
public class Object2{
private String obj2Name;
private String obj2Desc;
private List<Object3> object3List;
//Setters and getters
}
public class Object3{
private String obj3Name;
private String obj3Desc;
//Setters and getters
}
I wish to validate both name
and desc
in all objects, instead of using a nested loop like the following:
List<Object1> object1List = getObject1List();
for(Object1 object1 : object1List ){
if(object1.getObj1Name() == null){
//throw error
}
if(object1.getObj1Desc() == null){
//throw error
}
for(Object2 object2 : object1.getObject2List()){
if(object2.getObj2Name() == null){
//throw error
}
if(object2.getObj2Desc() == null){
//throw error
}
//loop Object 3 ...
}
}
Is there any better way to do it?