How to combine two methods in JAVA? -
i have 2 methods returns 2 values. method same thinking combine them single method , manipulate return different values not sure useful?
can tell me method can converted single ?
private boolean getfirst(list<apples> apples) { boolean isorange = false; if (apples != null) { (apples apple : apples) { string type = apple.getfruit(); boolean isapple = stringutils.equalsignorecase(type, orange); if (!isapple) { isorange = true; } } } return isorange; } private boolean getsecond(list<apples> apples) { boolean isappletype = false; if (apples != null) { (apples apple : apples) { string type = apple.getfruit(); boolean isapple = stringutils.equalsignorecase(type, orange); if (isapple) { isappletype = true; } } } return isappletype; }
you can use streams this:
list<apple> list = ...; // first method list.stream().anymatch((e) -> !stringutils.equalsignorecase(e.getfruit(), orange)); // second method list.stream().anymatch((e) -> stringutils.equalsignorecase(e.getfruit(), orange));
Comments
Post a Comment