I have two these methods. Their loops are pretty similar. I'd like to refactor it to avoid duplicate nested loops. How could I do that?@Overridepublic boolean isValid() {    for (final FormSection formSection : getFormSections()) {        for (final Component fieldComponent : formSection) {            boolean isFieldValid = ((Field)fieldComponent).isValid();            if(!isFieldValid){                return false;            }        }    }    return true;}private int countErrors() {    int errorAmount = 0;    for (final FormSection formSection : getFormSections()) {        for (final Component fieldComponent : formSection) {            boolean isFieldValid = ((Field)fieldComponent).isValid();            if(!isFieldValid){                errorAmount++;            }        }    }    return errorAmount;}I've tried to use interface but it doesn't look good.interface FieldHandler {    boolean process(AtomicInteger number);}@Overridepublic boolean isValid() {    return handleTextFieldsInTabs(new FieldHandler() {        @Override        public boolean process(AtomicInteger number) {            return false;        }    }, null);}boolean handleTextFieldsInTabs(FieldHandler fieldHandler, AtomicInteger number) {    for (final FormSection formSection : getFormSections()) {        for (final Component fieldComponent : formSection) {            boolean isFieldValid = ((Field)fieldComponent).isValid();            if(!isFieldValid && !fieldHandler.process(number)) {                return false;            }        }    }    return true;}private int countErrors() {    final AtomicInteger errorAmount = new AtomicInteger(0);    handleTextFieldsInTabs(new FieldHandler() {        @Override        public boolean process(AtomicInteger number) {            number.incrementAndGet();            return true;        }    }, errorAmount);    return errorAmount.intValue();}
Methods to check form validity and to count invalid form fields
java;validation;comparative review;form