敏捷工具
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

21 lines
659 B

2 years ago
  1. package com.whn.hellospring.utils;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.Optional;
  5. import java.util.stream.Collector;
  6. public class MyOptional {
  7. public static <T> List<T> toList(Optional<T> option) {
  8. return option.
  9. map(Collections::singletonList).
  10. orElse(Collections.emptyList());
  11. }
  12. public static <R, A, T> R collect(Optional<T> option, Collector<? super T, A, R> collector) {
  13. final A container = collector.supplier().get();
  14. option.ifPresent(v -> collector.accumulator().accept(container, v));
  15. return collector.finisher().apply(container);
  16. }
  17. }