1 2 3 | Option<String> str = Option.of("Hello"); str.map(String::length); str.flatMap(v -> Option.of(v.length())); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import io.vavr.control.Either; import java.util.concurrent.ThreadLocalRandom; public class Eithers { private static ThreadLocalRandom random = ThreadLocalRandom.current(); public static void main(String[] args) { Either<String, String> either = compute() .map(str -> str + " World") .mapLeft(Throwable::getMessage); System.out.println(either); } private static Either<Throwable, String> compute() { return random.nextBoolean() ? Either.left(new RuntimeException("Boom!")) : Either.right("Hello"); } } |
1 2 3 4 5 6 7 | Try<Integer> result = Try.of(() -> 1 / 0).recover(e -> 1); System.out.println(result); Try<String> lines = Try.of(() -> Files.readAllLines(Paths.get("1.txt"))) .map(list -> String.join(",", list)) .andThen((Consumer<String>) System.out::println); System.out.println(lines); |
1 2 3 4 5 | Lazy<BigInteger> lazy = Lazy.of(() -> BigInteger.valueOf(1024).pow(1024)); System.out.println(lazy.isEvaluated()); System.out.println(lazy.get()); System.out.println(lazy.isEvaluated()); |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |