首页  

Java8常用函数式接口     所属分类 java8 浏览量 295
函数式接口 只有一个抽象方法的接口


@FunctionalInterface
public interface Runnable {
    public abstract void run();
}

java.util.function.Function
java.util.function.Predicate
java.util.function.Consumer
java.util.function.Supplier


public interface Function<T, R> 
R apply(T t);

public interface Consumer<T> 
void accept(T t);
    
public interface Predicate<T> 
boolean test(T t);
    
public interface Supplier<T> 
T get();   


import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class FunctionInterfaceDemo {

    public static void main(String[] args) throws Exception {
        Function<String, Integer> function = (str) -> {
            return str.length();
        };
        System.out.println(function.apply("hello"));

        Predicate<String> predicate = (str) -> str.isEmpty();
        System.out.println(predicate.test("hello"));

        Consumer<String> consumer = (str) -> System.out.println(str);
        consumer.accept("hello");

        Supplier<String> supplier = () -> "hello";
        System.out.println(supplier.get());
        // 使用 return ,需要 { }
        supplier = () -> {
            return "hello";
        };
        System.out.println(supplier.get());

    }
}




上一篇     下一篇
aerospike prometheus grafana 监控

aerospike启动指定配置文件

Java8函数式接口

java8 lambda 原理及性能

aerospike 日志

aerospike ClusterStats 获取连接数信息