I want to filter the values with 0 to print message invalid but I get Optional.empty as result
Long l = 0L; Optional<String> l2 = Optional.of(l).filter(t -> t>0).map(t ->t.toString()); Optional<String>l3 =l2.filter(String::isEmpty).map(t ->"invalid"); System.out.println(l3); Need return type to be Optional<String>
Expected output - Invalid
Actual Output - Optional.empty
Updated
Need return type to be String
Expected output - Invalid value 0
Actual Output - Optional.empty
null) then Optional isn't right tool. Such cases should are better suited to be handled by simpleif(validate(value)){..handle correct value..} else {..handle incorrect value..}. For now your question looks like XY problem