sample.java
import java.util.Optional;
public class Main {
void exec(Optional<StringBuilder> pSb) {
System.out.println("pSb-->" + pSb);
StringBuilder sbEdit = pSb.orElseGet(() -> new StringBuilder("none")); //
System.out.println("orElseGet-->" + sbEdit ); -- "none"
//Edit arguments
Optional<String> os = pSb.map(sb -> {
sb.insert(0, "[");
sb.append("]");
return sb.toString();
});
}
public static void main(String... args) {
Main main = new Main();
// foo
StringBuilder sb = new StringBuilder("Jack");
sb = null;
main.exec(Optional.ofNullable(sb ));
System.out.println("return-->" + sb ); -- "[none]"
}
}
Recommended Posts