Convert Optional to boolean in functional style

  • A+

I just wanted to return a boolean from an Optional object by doing a check on the getProductType() on the ProductDetails object as shown below:

public boolean isElectronicProduct(String productName) {     Optional<ProductDetails> optProductDetails = findProductDetails(productName);     if(optProductDetails.isPresent()) {         return optProductDetails.get().getProductType() == ProductType.ELECTRONICS;     }     return false; } 

Intellij complains stating that the above code can be replaced in functional style, is there really any way to simplify the above Optional object and return a boolean? -> d.getProductType() == ProductType.ELECTRONICS) //Optional<Boolean>                  .orElse(false); //Boolean 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: