博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JDK8新特性
阅读量:4493 次
发布时间:2019-06-08

本文共 4908 字,大约阅读时间需要 16 分钟。

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

 

一、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
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
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
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
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
stream = Stream.of("张三", "李四", "王五", "赵六", "田七"); stream.forEach(name -> System.out.println(name)); }}

2.filter(底层是Predicate接口)

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

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

public class TestJdk8 {    public static void main(String[] args) {        ArrayList
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)); }}

6.collect(转换为集合)

List
platformChannelInfos = platformChannelPOs.stream() .map(platformChannelPO -> PlatformChannelPoConverter.po2Bo(platformChannelPO)) .collect(Collectors.toList());

 

三、方法引用

 1.sout

public class TestJdk8 {    public static void main(String[] args) {        ArrayList
list = new ArrayList<>(); list.add("美羊羊"); list.add("喜羊羊"); list.add("懒羊羊"); list.stream().forEach(System.out::println); }}

2.静态方法

public interface Calculate {    int calAbs(int num);}
public class TestJdk8 {    public static int method(int num, Calculate c) {        return c.calAbs(num);    }    public static void main(String[] args) {        System.out.println(method(-8, Math::abs));    }}

3.构造方法

public class Person {    private String name;    public Person() {    }    public Person(String name) {        this.name = name;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}
public interface PersonBuilder {    Person buildPerson(String name);}
public class TestJdk8 {    public static void buildPerson(String name, PersonBuilder pb) {        Person person = pb.buildPerson(name);        System.out.println(person + person.getName());    }    public static void main(String[] args) {        buildPerson("张学友", Person::new);    }}

4.构造数组

public interface ArrayBuilder {    int[] buildArray(int length);}
public class TestJdk8 {    public static int[] creatArray(int length, ArrayBuilder ab) {        return ab.buildArray(length);    }    public static void main(String[] args) {        int[] array = creatArray(12, int[]::new);        System.out.println(array.length);    }}

转载于:https://www.cnblogs.com/naixin007/p/11518614.html

你可能感兴趣的文章
Hive日期格式转换用法
查看>>
Ubuntu 14.04 安装 Xilinx ISE 14.7 全过程(转)
查看>>
Java基础的思维导图
查看>>
thinkphp+datatables+ajax 大量数据服务器端查询
查看>>
spring配置jax-ws
查看>>
ArrayList源码解析
查看>>
他们创造了美国 读书笔记
查看>>
SQL Server 2008 修改表所有者,架构
查看>>
literal在程序語言中的意思(轉)
查看>>
EnumClipboardFormats
查看>>
我最常用的Elipse快捷键
查看>>
单元测试(Unit Testing) 集成测试(Integrated Testing)系统测试(System Testing)
查看>>
DTO – 服务实现中的核心数据
查看>>
QWebView 与Js 交互
查看>>
linux脚本Shell之awk详解(二)
查看>>
转载-30分钟搞定后台登录界面(103个后台PSD源文件、素材网站)
查看>>
jxl导出Excel文件
查看>>
Oracle创建/删除表空间和用户(2014-3-10 记)
查看>>
springboot 初探 、基础及配置
查看>>
基于OWin的Web服务器Katana发布版本3
查看>>