Java Optional type

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

Java Optional type
Java type conversion
[JAVA] Stream type
[Java] Enumeration type
Studying Java 8 (Optional)
Java double type
[Java] Optional memorandum
Java 9 Optional :: stream
[Java] Data type ①-Basic type
View Java Optional Javadoc
[Java, Kotlin] Type Variance
Java class type field
Type determination in Java
Java study # 1 (typical type)
[Java] About enum type
[Java] Date type conversion
[Java] List type / Array type conversion
Rewrite Java try-catch with Optional
Java learning memo (data type)
Try functional type in Java! ①
How to use java Optional
[Java] Precautions for type conversion
[Java] Type conversion speed comparison
[Java] How to use Optional ②
Java study # 7 (branch syntax type)
Java
Java8 to start now ~ Optional ~
Java Primer Series (Type Conversion)
Java
[Java] How to use Optional ①
Compare Java 8 Optional with Swift
[Java] Data type / matrix product (AOJ ⑧ Matrix product)
Basic usage of java Optional Part 1
java (use class type for field)
[Java] Correct comparison of String type
Let's understand the Optional (Wrapped) type!
How to use Java enum type
Java learning (0)
[Java Siler] About type inference by var
Studying Java ―― 3
[Java] array
Conversion between Kotlin nullable and Java Optional
Java protected
[Java] Annotation
[Java] Module
[Java] Implicit type cast (AOJ10-sum of numbers)
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java date data type conversion (Date, Calendar, String)
Java methods
Java method
java (constructor)
Regarding String type equivalence comparison in Java
Java array
[Java] ArrayDeque
java (override)
[Easy-to-understand explanation! ] Reference type type conversion in Java
java (method)
Java Day 2018
Java string