用 java 8 stream flatMap 来做 fallback

今天 code review 的时候看到总监用 Java 8 stream 写了一个神奇的流程控制代码. 试了一下, 有点神奇, 像是不断处理, 每一步都可能失败, 最后有一个 fallback 来救场. 这里是一个示例:

// 保存为 StreamTest.java, 然后用下面两句编译运行
// $ javac StreamTest.java
// $ java StreamTest
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class StreamTest {
    static String testStreamFallback(Integer number) {
        String ret = Optional.ofNullable(number)
                .flatMap(n -> Optional.ofNullable(n % 5 == 0 ? null : n))
                .flatMap(n -> Optional.ofNullable(n % 3 == 0 ? null : n))
                .map(score -> score % 2 != 0 ? "not x2, x3, x5" : "not x3, x5")
                .orElse(number == null ? "null" : "x3 or x5");
        return String.format("%s is %s", number, ret);
    }

    public static void main(String[] args) {
        List numbers = Arrays.asList(
                0, 1, 2, 3, 4,
                5, 6, 7, 8, 9,
                null
        );
        System.out.println(String.join("\n",
                numbers.stream()
                        .map(i -> testStreamFallback(i))
                        .collect(Collectors.toList()))
        );
    }
}

编译运行:

▶ javac StreamTest.java

▶ java StreamTest
0 is x3 or x5
1 is not x2, x3, x5
2 is not x3, x5
3 is x3 or x5
4 is not x3, x5
5 is x3 or x5
6 is x3 or x5
7 is not x2, x3, x5
8 is not x3, x5
9 is x3 or x5
null is null

flatMap 的文档:

/**
 * If a value is present, apply the provided {@code Optional}-bearing
 * mapping function to it, return that result, otherwise return an empty
 * {@code Optional}.  This method is similar to {@link #map(Function)},
 * but the provided mapper is one whose result is already an {@code Optional},
 * and if invoked, {@code flatMap} does not wrap it with an additional
 * {@code Optional}.
 *
 * @param  The type parameter to the {@code Optional} returned by
 * @param mapper a mapping function to apply to the value, if present
 *           the mapping function
 * @return the result of applying an {@code Optional}-bearing mapping
 * function to the value of this {@code Optional}, if a value is present,
 * otherwise an empty {@code Optional}
 * @throws NullPointerException if the mapping function is null or returns
 * a null result
 */
public Optional flatMap(Function> mapper) {
    Objects.requireNonNull(mapper);
    if (!isPresent())
        return empty();
    else {
        return Objects.requireNonNull(mapper.apply(value));
    }
}

从 stream 的角度, 经过 mapper 的时候, 如果这个 optional 元素实际为 null, 返回 optional null (不作处理), 否则取出实际内容, 扔到 mapper 函数, 然后得到另一个 optional. 这个 stream 一直往后流, 直到 orElseGet.


说错了, 这不是 stream 的 fallback, 是 optional 的 fallback. Anyway, 不改了.


more to read:

  • What's the difference between map and flatMap methods in Java 8? - Stack Overflow
  • Java 8 flatMap example

你可能感兴趣的:(用 java 8 stream flatMap 来做 fallback)