java - Check whether there are remaining items in the List while iterating -
i'm doing bean validation list of conditions:
public abstract class basemyconditionvalidator<t extends annotation> implements constraintvalidator<t, list<mycondition>> { @override public void initialize(t constraintannotation) {} @override public boolean isvalid(list<mycondition> conditions, constraintvalidatorcontext context) { boolean result = true; if (!conditions.isempty()){ int = 0; (mycondition cond : conditions){ if (cond.getjoinpart() != null){ if (!hasnext(i, conditions)){ return false; } } i++; } } return result; } private boolean hasnext(int index, list<mycondition> conditions){ try { conditions.get(index + 1); } catch (exception e){ return false; } return true; } } my question is there simpler approach deal with:
- checking if there still item next in line during iteration of list
you use plain old iterator iterate through list, or instead of hasnext(i, conditions) check list length (i < conditions.size() - 1)
in end, instead of iterating through whole list, check if last element's joinpart null (at least doing)
@override public boolean isvalid(list<mycondition> conditions, constraintvalidatorcontext context) { return conditions.isempty() || conditions.get(conditions.size() - 1).getjoinpart() != null; }
Comments
Post a Comment