JAVA-Functional Interface

Counsumer interface

Pass in a parameter of type T, the return value is void

public void helpConsumer(){
    
    
    Consumer<String> con = new Consumer<String>() {
    
    
        @Override
        public void accept(String s) {
    
    
            System.out.println(s);      
        }
    };
    Consumer<String> consumer = s -> {
    
    
        System.out.println(s);
    };
}

Supplier interface

No parameter, method with return value type T

public void helpCSupplier() {
    
    
    Supplier<String> supplier = new Supplier<String>() {
    
    
        @Override
        public String get() {
    
    
            return "123";
        }
    };
    Supplier<String> sup = () -> {
    
    
        return "123";
    };
}

Function<T,R> interface

Pass in a parameter of type T and return a result of type R

public static void main(String[] args) {
    
    
    //传入字符串,求它的长度
    Function<String,Integer> fun = new Function<String, Integer>() {
    
    
        @Override
        public Integer apply(String s) {
    
    
            return s.length();
        }
    };
    Function<String, Integer> function = s -> {
    
    
        return s.length();
    };
}

Predicate interface

Pass in a parameter of type T and return a result of type boolean

public void helpPredicate(){
    
    
    Predicate<String> pre = new Predicate<String>() {
    
    
        @Override
        public boolean test(String s) {
    
    
            return s.isEmpty();
        }
    };
    Predicate<String> predicate = s -> {
    
    
       return s.isEmpty();
    };
}

Guess you like

Origin blog.csdn.net/weixin_42272869/article/details/112559146