跳转至

常用工具类

常用类#

  1. Supplier
    实现代码:

    1
    2
    3
    4
    @FunctionalInterface
    public interface Supplier<T> {
        T get();
    }
    

    使用:

    1
    2
    3
    4
    // get 等价于 类的构造函数
    Supplier<ConstructQuote> supplier = ConstructQuote::new;
    ConstructQuote constructQuote = supplier.get();
    Assert.assertTrue(constructQuote instanceof ConstructQuote);
    
  2. Predicate
    用于判断表达式是否正确

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    @FunctionalInterface
    public interface Predicate<T> {
    
        boolean test(T t);
    
        default Predicate<T> and(Predicate<? super T> other) {
            Objects.requireNonNull(other);
            return (t) -> test(t) && other.test(t);
        }
    
        default Predicate<T> negate() {
            return (t) -> !test(t);
        }
    
        default Predicate<T> or(Predicate<? super T> other) {
            Objects.requireNonNull(other);
            return (t) -> test(t) || other.test(t);
        }
    
        static <T> Predicate<T> isEqual(Object targetRef) {
            return (null == targetRef)
                    ? Objects::isNull
                    : object -> targetRef.equals(object);
        }
    }
    

    用法1

    1
    String s = new String("Java");
    

    Predicate predicate = s::startsWith; Assert.assertTrue(predicate.test("J")); 用法2

    1
    2
    3
    4
    List<String> strings = Arrays.asList("sum", "fei", "long", "sun", "long", "fei");
    Predicate<String> s1 = (s) -> s.startsWith("s");
    Predicate<String> s2 = (s) -> s.endsWith("n");
    strings.stream().filter(s1.and(s2)).forEach( s -> System.out.println(s));
    
  3. Function
    接收一个参数返回一个结果
    代码

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    @FunctionalInterface
    public interface Function<T, R> {
    
        R apply(T t);
    
        default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
            Objects.requireNonNull(before);
            return (V v) -> apply(before.apply(v));
        }
    
        default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
            Objects.requireNonNull(after);
            return (T t) -> after.apply(apply(t));
        }
    
        static <T> Function<T, T> identity() {
            return t -> t;
        }
    }
    

    用法

    1
    List<Integer> costs = Arrays.asList(100, 200, 300, 400, 500);
    

    costs.stream().map(cost -> cost + cost*0.12).forEach(x -> System.out.println(x)); 4. Consumer
    接收一个参数,然后
    实现代码:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    @FunctionalInterface
    public interface Consumer<T> {
    
        void accept(T t);
    
        default Consumer<T> andThen(Consumer<? super T> after) {
            Objects.requireNonNull(after);
            return (T t) -> { accept(t); after.accept(t); };
        }
    }
    

    用法

    1
    List<Integer> costs = Arrays.asList(100, 200, 300, 400, 500);
    

    costs.stream().map(cost -> cost + cost*0.12).forEach(x -> System.out.println(x));

  4. BiFunction
    接收两个参数,并返回处理结果
    实现代码

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    @FunctionalInterface
    public interface BiFunction<T, U, R> {
    
        R apply(T t, U u);
    
        default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {
            Objects.requireNonNull(after);
            return (T t, U u) -> after.apply(apply(t, u));
        }
    }