This method first will check whether our Optional object is empty or not? If not empty, convert its value to another value. Similar to the filter() method, this method returns an Optional object with a value after conversion. For example:
|
package com.huongdanjava.javaexample; import java.util.Optional; public class Example { public static void main(String[] args) { String s = new String("Khanh"); Optional<String> opt = Optional.ofNullable(s); opt.map(x -> x.toLowerCase()) .filter(x -> x.contains("k")) .ifPresent(x -> System.out.println("Name: " + x)); } } |
In the above example,… Read More