coder25 :
I am trying to migrate the below code to java 8
private boolean test(String id1, String id2) {
if(id2== null || id2.isEmpty()) return true;
return id1.equals(id2);
}
Solution tried
Optional.ofNullable(id2).map(String::isEmpty).orElse(id2.equals(id1));
The above solution doesnot work when
id2 is null
id2 is equal to id1
Ousmane D. :
Optional's are not meant to replace simple if
checks. You're essentially "migrating" to a less readable and efficient solution.
If I were you I'd stick with your current solution as it's the better option, but if you want to code golf a little bit then you can do as @Holger has shown in the comments:
return id2 == null || id2.isEmpty() || id2.equals(id1);