首页 > 其他 > 详细

JDK8新特性

时间:2019-09-14 12:13:48      阅读:69      评论:0      收藏:0      [点我收藏+]

函数式接口:只有一个抽象方法的接口

 

一、lambda表达式(方法参数为函数式接口)

1.无参

public interface MyFunctionalInterface {
    void method();
}
public class TestJdk8 {
    public static void show(MyFunctionalInterface myInter) {
        myInter.method();
    }

    public static void main(String[] args) {
        show(() -> System.out.println("使用无参lambda表达式"));
    }
}

2.创建线程

public class TestJdk8 {
    public static void startThread(Runnable runnable) {
        new Thread(runnable).start();
    }

    public static void main(String[] args) {
        startThread(() -> System.out.println(Thread.currentThread().getName() + " --> 线程启动了"));
    }
}

3.Supplier接口

public class TestJdk8 {
    public static String getStr(Supplier<String> sup) {
        return sup.get();
    }

    public static void main(String[] args) {
        String str = getStr(() -> "头条");
        System.out.println(str);
    }
}

 4.Consumer接口

public class TestJdk8 {
    public static void method(String name, Consumer<String> con) {
        con.accept(name);
    }

    public static void main(String[] args) {
        method("百度", name -> System.out.println(name));
    }
}

5.Predicate接口

public class TestJdk8 {
    public static boolean checkStr(String str, Predicate<String> pre) {
        return pre.test(str);
    }

    public static void main(String[] args) {
        boolean b = checkStr("abcde", str -> str.length() > 5);
        System.out.println(b);
    }
}

6.Function接口(类型转换)

public class TestJdk8 {
    public static void change(String str, Function<String, Integer> fun) {
        Integer i = fun.apply(str);
        System.out.println(i + 1);
    }

    public static void main(String[] args) {
        change("1234", s -> Integer.parseInt(s));
    }
}

 

二、stream流

1.foreach

public class TestJdk8 {
    public static void main(String[] args) {
        Stream<String> stream = Stream.of("张三", "李四", "王五", "赵六", "田七");
        stream.forEach(name -> System.out.println(name));
    }
}

2.filter(底层是Predicate接口)

public class TestJdk8 {
    public static void main(String[] args) {
        Stream<String> stream = Stream.of("张三丰", "张无忌", "赵敏", "周芷若", "张翠山", "张三");
        stream.filter(name -> name.startsWith("张"))
                .filter(name -> name.length() > 2)
                .forEach(name -> System.out.println(name));
    }
}

3.map(底层是Function接口)

public class TestJdk8 {
    public static void main(String[] args) {
        Stream<String> stream = Stream.of("1", "2", "3", "4");
        stream.map(s -> Integer.parseInt(s) + 2)
                .forEach(i -> System.out.println(i));
    }
}

4.count

public class TestJdk8 {
    public static void main(String[] args) {
        Stream<String> stream = Stream.of("张三丰", "张无忌", "赵敏", "周芷若", "张翠山", "张三");
        System.out.println(stream.count());
    }
}

5.limit(skip方法与之相反,不再赘述)

public class TestJdk8 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("美羊羊");
        list.add("喜羊羊");
        list.add("懒羊羊");
        list.add("沸羊羊");
        list.add("灰太狼");
        list.add("红太狼");
        list.stream().limit(5).forEach(s -> System.out.println(s));
    }
}

 

三、方法引用

 

JDK8新特性

原文:https://www.cnblogs.com/naixin007/p/11518614.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!