View Java Optional Javadoc

what

-Look at the Optional Javadoc on the premise of previous knowledge --Viewed version: Java8 -Optional javadoc --SpaceDog-> Dog-> Animal-> Object inheritance relationship

public Optional<T> filter(Predicate<? super T> predicate) --filter takes a Predicate with an implementation of "boolean test (T t)" as an argument --predicate receives an instance of type T and returns the truth. At that time, the upper class of T may be used for the judgment instead of T. --Specific example) When filtering Optional \ , there is no problem even if the method targets Dog, Animal, Object as a test.

public T orElseGet(Supplier<? extends T> other) --orElseGet takes a Supplier with an implementation of "T get ()" as an argument --other returns an instance of type T. At that time, it may be a lower class of T instead of T. --Specific example) There is no problem with the method that returns SpaceDog when performing orElseGet of Optional \ .

public void ifPresent(Consumer<? super T> consumer) --ifPresent takes a Consumer with an implementation of "void accept (T t)" as an argument --Consumer receives an instance of type T and returns nothing. At that time, the upper class of T may be used instead of T. --Specific example) When IfPresent Optional \ , there is no problem even if Dog, Animal, Object is accepted.

public <U> Optional<U> map(Function<? super T,? extends U> mapper) --map takes a Function with an implementation of "U apply (T t)" as an argument --mapper takes a T-type instance as an argument and returns a U-type instance. At that time, the upper class of T may be used instead of T, and the lower class of U may be used instead of U (a rule that does not matter even if the consumer side interprets the upper class and the production side interprets the lower class). --mapper converts from T (or higher) to U (or lower)

public <U> Optional<U> flatMap(Function<? super T,Optional<U>> mapper) --flatMap takes a Function with an implementation of "U apply (T t)" as an argument --mapper takes an instance of T type as an argument and returns Optional \ type. At that time, the upper class of T may be used instead of T. --At this time, Optional \ is not Optional \ <? Extends U> (this is an issue) --Addition) I was told in the comments that Java 9 has changed to Optional \ <? Extends U>.

Function<Integer, Optional<SpaceDog>> myConverter = new Function<Integer, Optional<SpaceDog>>() {
    @Override
    public Optional<SpaceDog> apply(Integer t) {
        SpaceDog d = new SpaceDog();
        d.age = t;
        return Optional.of(d);
    }
};

//Compile error in Java8, OK in Java9
Optional<Dog> optDog = optInt.flatMap(myConverter);

public T orElse(T other) --orElse takes a T-type instance as an argument ――Is this the same without border parameters? -Is it okay because it can be used as a subtype if it is not a generic type?

	Optional<Dog> opt = Optional.of(new Dog());
	Dog d = opt.orElse(new SpaceDog());

--Addition) In response to the problem that orElse cannot be used with the boundary type parameter Optional, I thought of an improvement plan if Optional had such a static method.


	/**
	 *Method to enable Optional orElse with optional variable of boundary type parameter
	 * 
	 * @Producer of param optT T(May produce a lower class of T)
	 * @param other U type with the constraint of "T or a subclass of T"
	 * @return Return as T type
	 */
	static <T, U extends T> T orElseEx(Optional<? extends T> optT, U other) { 
		if(optT.isPresent()) {
			return optT.get();
		}
		return other;
	}

//Example of use
Optional<? extends Number> optNum = Optional.empty();
Number num2 = MyOptional.orElseEx(optNum, 0.5);
System.out.println(num2); //0.5

Recommended Posts