jdk都出到16了8还没掌握吗?挽周带你玩转java8新特性(建议收藏)

  • 友情链接

    • 一.Lambda表达式
  • 二.函数式(Funcation)接口

  • 三.方法引用与构造器引用

    • 方法引用
  • 构造器引用

  • 数组引用

  • 四.Stream API

  • 五.Optional类


友情链接

=======================================================================

菜鸟教程:点我跳转Java8新特性菜鸟教程地址

JDK:点我跳转Jdk8官网下载地址


一.Lambda表达式


Lambda 是一个匿名函数,我们可以把Lambda表达式理解为是一段可以传递的代码,将代码像数据一样进行传递。使用它可以写出更简洁,更灵活的代码。作为一种更紧凑的代码风格。使Java的语言表达能力得到了提升。

语法:

(o1,o2) -> Integer.compare(o1,o2);

  • ->:lambda操作符,也叫箭头操作符

  • -> 左边:lambda形参列表 (其实就是接口中的抽象方法的形参列表)

  • -> 右边:lambda体(其实就是重写的抽象方法的方法体)

案例:

  1. 格式:无参数,无返回值

@Test

public void test(){

//原写法

Runnable runnable=new Runnable() {

@Override

public void run() {

System.out.println(“无参无返回值原写法”);

}

};

runnable.run();

//Lambda表达式写法

Runnable lrunnable =()->{System.out.println(“无参无返回值Lambda表达式写法”);};

lrunnable.run();

}

在这里插入图片描述

  1. 格式:Lambda 需要一个参数,但是没有返回值

@Test

public void test(){

//原写法

Consumer consumer=new Consumer() {

@Override

public void accept(String s) {

System.out.println(s);

}

};

consumer.accept(“需要一个参数没有返回值,原写法”);

//Lambda表达式写法

Consumer lconsumer =(String s)->{System.out.println(s);};

lconsumer.accept(“需要一个参数没有返回值,Lambda表达式写法”);

}

在这里插入图片描述

  1. 格式:数据类型可以省略,因为可由编译器推断得出,称为“类型推断”

@Test

public void test(){

//原写法

Consumer consumer=new Consumer() {

@Override

public void accept(String s) {

System.out.println(s);

}

};

consumer.accept(“需要一个参数没有返回值,原写法”);

//Lambda表达式写法

Consumer lconsumer =(s)->{System.out.println(s);};

lconsumer.accept(“需要一个参数没有返回值并且数据类型可以省略, Lambda表达式写法”);

}

在这里插入图片描述

  1. 格式:Lambda 若只需要一个参数时,参数的小括号可以省略

@Test

public void test(){

//原写法

Consumer consumer=new Consumer() {

@Override

public void accept(String s) {

System.out.println(s);

}

};

consumer.accept(“需要一个参数没有返回值,原写法”);

//Lambda表达式写法

Consumer lconsumer = s ->{System.out.println(s);};

lconsumer.accept(“需要一个参数没有返回值,数据类型可以省略,小括号也可以省略 Lambda表达式写法”);

}

在这里插入图片描述

  1. 格式:Lambda 需要两个或以上的参数,多条执行语句,并且可以有返回值

@Test

public void test(){

//原写法

Comparator comparator=new Comparator() {

@Override

public int compare(Integer o1, Integer o2) {

return o1.compareTo(o2);

}

};

System.out.println(comparator.compare(1,2));

//Lambda表达式写法

Comparator lcomparator=(o1,o2)->{return o1.compareTo(o2);};

System.out.println(lcomparator.compare(1,2));

}

在这里插入图片描述

  1. 格式:当 Lambda 体只有一条语句时, return 与大括号都可以省略

@Test

public void test(){

//原写法

Comparator comparator=new Comparator() {

@Override

public int compare(Integer o1, Integer o2) {

return o1.compareTo(o2);

}

};

System.out.println(comparator.compare(1,2));

//Lambda表达式写法

Comparator lcomparator=(o1,o2)-> o1.compareTo(o2);

System.out.println(lcomparator.compare(1,2));

}

在这里插入图片描述

总结:

  • lambda表达式的本质:作为函数式接口的实例

  • -> 左边 : 如果使用泛型,lambda形参列表的参数类型可以省略(类型推断),如果lambda形参列表只有一个参数()也可以省略。

  • -> 右边:lambda体使用一对{}包裹,如果lambda体只有一条执行语句(可能是return语句),可以省略这一对{}return关键字 (如果省略了return关键字{}也一定要去掉)


二.函数式(Funcation)接口


如果一个接口中,只声明了一个抽象方法(可以有多个非抽象方法),则此接口就称为函数式接口,函数式接口可以被隐式转换为 lambda 表达式。

学习之前先来看一下JDK 1.8 之前已有的函数式接口 Runnable

在这里插入图片描述

我们常用的一些接口Callable、Runnable、Comparator等在JDK8中都添加了@FunctionalInterface注解。该注解不是必须的,如果一个接口符合"函数式接口"定义,那么加不加该注解都没有影响。加上该注解能够更好地让编译器进行检查。如果编写的不是函数式接口,但是加上@FunctionInterface,那么编译器会报错。

自定义函数式接口:

/**

  • @author mengzhichao

  • @create 2021-06-06-18:25

*/

@FunctionalInterface

public interface MyInterface {

void method();

}

总结:在Java8中,Lambda表达式就是一个函数式接口的实例。这就是Lambda表达式和函数式接口的关系。也就是说,只要一个对象是函数式接口的实例。那么该对象就可以用Lambda表达式来表示。所以以前用匿名实现类表示的现在都可以用Lambda表达式来写。

JDK 1.8 新增加的函数接口:

| 接口 | 描述 |

| — | — |

| BiConsumer<T,U> | 代表了一个接受两个输入参数的操作,并且不返回任何结果 |

| BiFunction<T,U,R> | 代表了一个接受两个输入参数的方法,并且返回一个结果 |

| BinaryOperator< T > | 代表了一个作用于于两个同类型操作符的操作,并且返回了操作符同类型的结果 |

| BiPredicate<T,U> | 代表了一个两个参数的boolean值方法 |

| BooleanSupplier | 代表了boolean值结果的提供方 |

| Consumer | 代表了接受一个输入参数并且无返回的操作 |

| DoubleBinaryOperator | 代表了作用于两个double值操作符的操作,并且返回了一个double值的结果。 |

| DoubleConsumer | 代表一个接受double值参数的操作,并且不返回结果。 |

| DoubleFunction< R > | 代表接受一个double值参数的方法,并且返回结果 |

| DoublePredicate | 代表一个拥有double值参数的boolean值方法 |

| DoubleSupplier | 代表一个double值结构的提供方 |

| DoubleToIntFunction | 接受一个double类型输入,返回一个int类型结果。 |

| DoubleToLongFunction | 接受一个double类型输入,返回一个long类型结果 |

| DoubleUnaryOperator | 接受一个参数同为类型double,返回值类型也为double 。 |

| Function<T,R> | 接受一个输入参数,返回一个结果。 |

| IntBinaryOperator | 接受两个参数同为类型int,返回值类型也为int 。 |

| IntConsumer | 接受一个int类型的输入参数,无返回值 。 |

| IntFunction< R > | 接受一个int类型输入参数,返回一个结果 。 |

| IntPredicate | 接受一个int输入参数,返回一个布尔值的结果。 |

| IntSupplier | 无参数,返回一个int类型结果。 |

| IntToDoubleFunction | 接受一个int类型输入,返回一个double类型结果 。 |

| IntToLongFunction | 接受一个int类型输入,返回一个long类型结果。 |

| IntUnaryOperator | 接受一个参数同为类型int,返回值类型也为int 。 |

| LongBinaryOperator | 接受两个参数同为类型long,返回值类型也为long。 |

| LongConsumer | 接受一个long类型的输入参数,无返回值。 |

| LongFunction< R > | 接受一个long类型输入参数,返回一个结果。 |

| LongPredicate | R接受一个long输入参数,返回一个布尔值类型结果。 |

| LongSupplier | 无参数,返回一个结果long类型的值。 |

| LongToDoubleFunction | 接受一个long类型输入,返回一个double类型结果。 |

| LongToIntFunction | 接受一个long类型输入,返回一个int类型结果。 |

| LongUnaryOperator | 接受一个参数同为类型long,返回值类型也为long。 |

| ObjDoubleConsumer< T > | 接受一个object类型和一个double类型的输入参数,无返回值。 |

| ObjIntConsumer< T > | 接受一个object类型和一个int类型的输入参数,无返回值。 |

| ObjLongConsumer< T > | 接受一个object类型和一个long类型的输入参数,无返回值。 |

| Predicate< T > | 接受一个输入参数,返回一个布尔值结果。 |

| Supplier< T > | 无参数,返回一个结果。 |

| ToDoubleBiFunction<T,U> | 接受两个输入参数,返回一个double类型结果 |

| ToDoubleFunction< T > | 接受一个输入参数,返回一个double类型结果 |

| ToIntBiFunction<T,U> | 接受两个输入参数,返回一个int类型结果。 |

| ToIntFunction< T > | 接受一个输入参数,返回一个int类型结果。 |

| ToLongBiFunction<T,U> | 接受两个输入参数,返回一个long类型结果。 |

| ToLongFunction< T > | 接受一个输入参数,返回一个long类型结果。 |

| UnaryOperator< T > | 接受一个参数为类型T,返回值类型也为T。 |

案例(再此只演示极个别常用的,小伙伴们私底下可以自行练习):

  • consumer即消费接口,传入一个参数,并对其进行相应的操作

public class LambdaTest {

public void happyTime(double money,Consumer consumer){

consumer.accept(money);

}

@Test

public void test(){

//原写法

happyTime(500, new Consumer() {

@Override

public void accept(Double aDouble) {

System.out.println(“原写法,价格为:”+aDouble);

}

});

//Lambda表达式写法

happyTime(500,money-> System.out.println(“Lambda表达式写法,价格为:”+money));

}

}

在这里插入图片描述

  • supplier即供给接口,可以传入数据,作为一个容器;

public class LambdaTest {

@Test

public void test(){

//原写法

Supplier supplier=new Supplier() {

@Override

public String get() {

return “原写法,供给型接口”;

}

};

System.out.println(supplier.get());

//Lambda表达式写法

Supplier lSupplier=()->“lambda写法,供给型接口”;

System.out.println(lSupplier.get());

}

}

在这里插入图片描述

  • function即方法接口,主要是用作数据类型之间的转换;

public class LambdaTest {

@Test

public void test(){

//原写法(给一个int 转成字符串并返回)

Function<Integer,String> function=new Function<Integer, String>() {

@Override

public String apply(Integer integer) {

return String.valueOf(integer);

}

};

System.out.println(function.apply(10).getClass().getName());

//Lambda表达式写法

Function<Integer,String> lFunction=(integer) -> String.valueOf(integer);

System.out.println(lFunction.apply(20).getClass().getName());

}

}

在这里插入图片描述

  • predicate即判断接口,传入参数,而后返回判断的结果true/false;

public class LambdaTest {

//根据给定的规则,过滤集合中的字符串。此规则由Predicate的方法决定

public List filterString(List list, Predicate predicate){

List filterList=new ArrayList<>();

for (String s:list){

if (predicate.test(s)){

filterList.add(s);

}

}

return filterList;

}

@Test

public void test(){

//原写法

List list=Arrays.asList(new String[]{“上海”, “香港”, “澳门”, “曹县”});

List filterString = filterString(list, new Predicate() {

@Override

public boolean test(String s) {

return “曹县”.equals(s) ? true : false;

}

});

System.out.println(filterString);

//Lambda表达式写法

List lfilterString = filterString(list, s -> “上海”.equals(s) ? true : false);

System.out.println(lfilterString);

}

}

在这里插入图片描述


三.方法引用与构造器引用


方法引用

当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用 ! 方法引用可以看作是Lambda表达式深层次的表达,换句话说,方法引用就是Lambda表达式,也就是函数式接口的一个实例,通过方法名字来指向一个方法,可以认为是Lambda表达式的一个语法糖。

语法:

使用操作符 “::” 将类(或对象)与方法名分割开来。

要求:实现接口的抽象方法的参数和返回值类型,必须与方法引用的方法的参数列表和返回值类型保持一致!

案例:

  1. 格式:对象 : : 非静态方法

@Test

public void test(){

//Lambda表达式写法

Consumer consumer = str ->System.out.println(str);

consumer.accept(“Lambda表达式”);

//方法引用写法

Consumer fConsumer = System.out::println;

fConsumer.accept(“方法引用”);

}

在这里插入图片描述

  1. 格式:类 : : 静态方法

public class LambdaTest {

@Test

public void test(){

//Lambda表达式写法 比较两个数大小

Comparator comparator =(t1,t2) -> Integer.compare(t1,t2);

System.out.println(comparator.compare(10,20));

//方法引用写法

Comparator fComparator = Integer::compare;

System.out.println(fComparator.compare(20,10));

}

}

在这里插入图片描述

  1. 格式:类 : : 非静态方法

在这里插入图片描述

在这里插入图片描述

public class LambdaTest {

@Test

public void test(){

//Lambda表达式写法

Comparator comparator=(s1,s2) -> s1.compareTo(s2);

System.out.println(comparator.compare(“abc”,“abd”));

//方法引用写法

Comparator fComparator = String::compareTo;

System.out.println(fComparator.compare(“www”,“www”));

}

}

在这里插入图片描述

正常来说comparator.compare("abc","abd")有两个参数,而s1.compareTo(s2)只有一个参数并不满足上述所说的方法引用的要求。但是为什么能用呢?因为这种情况下,第一个参数作为方法的调用者出现((s1,s2) -> s1.compareTo(s2)),这种情况下也存在方法引用。只不过这时候我们不是拿具体的对象写,而是拿它的类写(Comparator<String> fComparator = String::compareTo;)

构造器引用

调用的构造器会根据实现的函数接口的抽象方法的参数列表来确定,如果抽象方法为无参的,则调用的构造器也是无参的。

语法:

ClassName :: new

案例

public class Student {

public Student() {

System.out.println(“Student 无参构造已执行”);

}

public Student(Integer id) {

System.out.println(“Student 有参构造,一个参数.为:”+id);

}

public Student(Integer id, String name, char sex) {

this.id = id;

this.name = name;

this.sex = sex;

}

private Integer id;

private String name;

private char sex;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public char getsex() {

return sex;

}

public void setsex(char sex) {

this.sex = sex;

}

@Override

public String toString() {

return “Student{” +

“id=” + id +

“, name='” + name + ‘’’ +

“, sex=” + sex +

‘}’;

}

}

  1. 通过无参构造器引用(创建对象)

public class LambdaTest {

@Test

public void test(){

//原始写法

Supplier supplier =new Supplier() {

@Override

public Student get() {

return new Student();

}

};

supplier.get();

//lambda表达式写法

Supplier lSupplier =()-> new Student();

lSupplier.get();

//构造器引用写法

Supplier gSupplier = Student::new;

gSupplier.get();

}

}

在这里插入图片描述

  1. 通过有参构造器引用(一个参数,创建对象)

@Test

public void test(){

//原始写法

Function<Integer,Student> function =new Function<Integer, Student>() {

@Override

public Student apply(Integer integer) {

return new Student(integer);

}

};

function.apply(1);

//lambda表达式写法

Function<Integer,Student> lFunction = id -> new Student(id);

lFunction.apply(2);

//构造器引用写法

Function<Integer,Student> gFunction = Student::new;

gFunction.apply(3);

}

在这里插入图片描述

数组引用

大家可以把数组看做是一个特殊的类,写法与构造器引用一致。

public class LambdaTest {

@Test

public void test(){

//原始写法

Function<Integer,String[]> function =new Function<Integer, String[]>() {

@Override

public String[] apply(Integer integer) {

return new String[integer];

}

};

System.out.println(Arrays.toString(function.apply(10)));

//lambda表达式写法

Function<Integer,String[]> lFunction= length -> new String[length];

System.out.println(Arrays.toString(lFunction.apply(20)));

//构造器引用写法

Function<Integer,String[]> gFunction = String[] :: new;

System.out.println(Arrays.toString(gFunction.apply(30)));

}

}

在这里插入图片描述


四.Stream API


Java8中有两大最为重要的改变。第一个是Lambda表达式;另外一个则是Stream API

Stream API(java.util.Stream) 把真正的函数式编程风格引入到Java中。这是目前为止对Java类库最好的补充,因为Stream API 可以极大提供Java程序员的生产力,让程序员写出高效率,干净,简洁的代码。

(Stream 使用一种类似用 SQL 语句从数据库查询数据的直观方式来提供一种对 Java 集合运算和表达的高阶抽象。)

Strema到底是什么呢?

是数据渠道,用于操作数据源(集合,数组等) 所生成的元素序列。

“集合将的是数据,Stream讲的是计算!”

语法

集合或数组.stream().过滤().映射().终止操作

Stream 操作的三个步骤

  1. 创建 Stream

一个数据源(集合,数组),获取一个流

  1. 中间操作

一个中间操作链,对数据源的数据进行处理

  1. 终止操作(终端操作)

一旦执行终止操作,就执行中间操作链,并产生结果。之后,不会再被使用

#mermaid-svg-8pLghYUGT1rUbUma .label{font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family);fill:#333;color:#333}#mermaid-svg-8pLghYUGT1rUbUma .label text{fill:#333}#mermaid-svg-8pLghYUGT1rUbUma .node rect,#mermaid-svg-8pLghYUGT1rUbUma .node circle,#mermaid-svg-8pLghYUGT1rUbUma .node ellipse,#mermaid-svg-8pLghYUGT1rUbUma .node polygon,#mermaid-svg-8pLghYUGT1rUbUma .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-8pLghYUGT1rUbUma .node .label{text-align:center;fill:#333}#mermaid-svg-8pLghYUGT1rUbUma .node.clickable{cursor:pointer}#mermaid-svg-8pLghYUGT1rUbUma .arrowheadPath{fill:#333}#mermaid-svg-8pLghYUGT1rUbUma .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-8pLghYUGT1rUbUma .flowchart-link{stroke:#333;fill:none}#mermaid-svg-8pLghYUGT1rUbUma .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-8pLghYUGT1rUbUma .edgeLabel rect{opacity:0.9}#mermaid-svg-8pLghYUGT1rUbUma .edgeLabel span{color:#333}#mermaid-svg-8pLghYUGT1rUbUma .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-8pLghYUGT1rUbUma .cluster text{fill:#333}#mermaid-svg-8pLghYUGT1rUbUma div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-8pLghYUGT1rUbUma .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-8pLghYUGT1rUbUma text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-8pLghYUGT1rUbUma .actor-line{stroke:grey}#mermaid-svg-8pLghYUGT1rUbUma .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-8pLghYUGT1rUbUma .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-8pLghYUGT1rUbUma #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-8pLghYUGT1rUbUma .sequenceNumber{fill:#fff}#mermaid-svg-8pLghYUGT1rUbUma #sequencenumber{fill:#333}#mermaid-svg-8pLghYUGT1rUbUma #crosshead path{fill:#333;stroke:#333}#mermaid-svg-8pLghYUGT1rUbUma .messageText{fill:#333;stroke:#333}#mermaid-svg-8pLghYUGT1rUbUma .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-8pLghYUGT1rUbUma .labelText,#mermaid-svg-8pLghYUGT1rUbUma .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-8pLghYUGT1rUbUma .loopText,#mermaid-svg-8pLghYUGT1rUbUma .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-8pLghYUGT1rUbUma .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-8pLghYUGT1rUbUma .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-8pLghYUGT1rUbUma .noteText,#mermaid-svg-8pLghYUGT1rUbUma .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-8pLghYUGT1rUbUma .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-8pLghYUGT1rUbUma .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-8pLghYUGT1rUbUma .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-8pLghYUGT1rUbUma .mermaid-main-font{font-family:“trebuchet ms”, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-8pLghYUGT1rUbUma .section{stroke:none;opacity:0.2}#mermaid-svg-8pLghYUGT1rUbUma .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-8pLghYUGT1rUbUma .section2{fill:#fff400}#mermaid-svg-8pLghYUGT1rUbUma .section1,#mermaid-svg-8pLghYUGT1rUbUma .section3{fill:#fff;opacity:0.2}#mermaid-svg-8pLghYUGT1rUbUma .sectionTitle0{fill:#333}#mermaid-svg-8pLghYUGT1rUbUma .sectionTitle1{fill:#333}#mermaid-svg-8pLghYUGT1rUbUma .sectionTitle2{fill:#333}#mermaid-svg-8pLghYUGT1rUbUma .sectionTitle3{fill:#333}#mermaid-svg-8pLghYUGT1rUbUma .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-8pLghYUGT1rUbUma .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-8pLghYUGT1rUbUma .grid .tick text{font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-8pLghYUGT1rUbUma .grid path{stroke-width:0}#mermaid-svg-8pLghYUGT1rUbUma .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-8pLghYUGT1rUbUma .task{stroke-width:2}#mermaid-svg-8pLghYUGT1rUbUma .taskText{text-anchor:middle;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-8pLghYUGT1rUbUma .taskText:not([font-size]){font-size:11px}#mermaid-svg-8pLghYUGT1rUbUma .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-8pLghYUGT1rUbUma .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-8pLghYUGT1rUbUma .task.clickable{cursor:pointer}#mermaid-svg-8pLghYUGT1rUbUma .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-8pLghYUGT1rUbUma .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-8pLghYUGT1rUbUma .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-8pLghYUGT1rUbUma .taskText0,#mermaid-svg-8pLghYUGT1rUbUma .taskText1,#mermaid-svg-8pLghYUGT1rUbUma .taskText2,#mermaid-svg-8pLghYUGT1rUbUma .taskText3{fill:#fff}#mermaid-svg-8pLghYUGT1rUbUma .task0,#mermaid-svg-8pLghYUGT1rUbUma .task1,#mermaid-svg-8pLghYUGT1rUbUma .task2,#mermaid-svg-8pLghYUGT1rUbUma .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-8pLghYUGT1rUbUma .taskTextOutside0,#mermaid-svg-8pLghYUGT1rUbUma .taskTextOutside2{fill:#000}#mermaid-svg-8pLghYUGT1rUbUma .taskTextOutside1,#mermaid-svg-8pLghYUGT1rUbUma .taskTextOutside3{fill:#000}#mermaid-svg-8pLghYUGT1rUbUma .active0,#mermaid-svg-8pLghYUGT1rUbUma .active1,#mermaid-svg-8pLghYUGT1rUbUma .active2,#mermaid-svg-8pLghYUGT1rUbUma .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-8pLghYUGT1rUbUma .activeText0,#mermaid-svg-8pLghYUGT1rUbUma .activeText1,#mermaid-svg-8pLghYUGT1rUbUma .activeText2,#mermaid-svg-8pLghYUGT1rUbUma .activeText3{fill:#000 !important}#mermaid-svg-8pLghYUGT1rUbUma .done0,#mermaid-svg-8pLghYUGT1rUbUma .done1,#mermaid-svg-8pLghYUGT1rUbUma .done2,#mermaid-svg-8pLghYUGT1rUbUma .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-8pLghYUGT1rUbUma .doneText0,#mermaid-svg-8pLghYUGT1rUbUma .doneText1,#mermaid-svg-8pLghYUGT1rUbUma .doneText2,#mermaid-svg-8pLghYUGT1rUbUma .doneText3{fill:#000 !important}#mermaid-svg-8pLghYUGT1rUbUma .crit0,#mermaid-svg-8pLghYUGT1rUbUma .crit1,#mermaid-svg-8pLghYUGT1rUbUma .crit2,#mermaid-svg-8pLghYUGT1rUbUma .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-8pLghYUGT1rUbUma .activeCrit0,#mermaid-svg-8pLghYUGT1rUbUma .activeCrit1,#mermaid-svg-8pLghYUGT1rUbUma .activeCrit2,#mermaid-svg-8pLghYUGT1rUbUma .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-8pLghYUGT1rUbUma .doneCrit0,#mermaid-svg-8pLghYUGT1rUbUma .doneCrit1,#mermaid-svg-8pLghYUGT1rUbUma .doneCrit2,#mermaid-svg-8pLghYUGT1rUbUma .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-8pLghYUGT1rUbUma .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-8pLghYUGT1rUbUma .milestoneText{font-style:italic}#mermaid-svg-8pLghYUGT1rUbUma .doneCritText0,#mermaid-svg-8pLghYUGT1rUbUma .doneCritText1,#mermaid-svg-8pLghYUGT1rUbUma .doneCritText2,#mermaid-svg-8pLghYUGT1rUbUma .doneCritText3{fill:#000 !important}#mermaid-svg-8pLghYUGT1rUbUma .activeCritText0,#mermaid-svg-8pLghYUGT1rUbUma .activeCritText1,#mermaid-svg-8pLghYUGT1rUbUma .activeCritText2,#mermaid-svg-8pLghYUGT1rUbUma .activeCritText3{fill:#000 !important}#mermaid-svg-8pLghYUGT1rUbUma .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-8pLghYUGT1rUbUma g.classGroup text{fill:#9370db;stroke:none;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family);font-size:10px}#mermaid-svg-8pLghYUGT1rUbUma g.classGroup text .title{font-weight:bolder}#mermaid-svg-8pLghYUGT1rUbUma g.clickable{cursor:pointer}#mermaid-svg-8pLghYUGT1rUbUma g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-8pLghYUGT1rUbUma g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-8pLghYUGT1rUbUma .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-8pLghYUGT1rUbUma .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-8pLghYUGT1rUbUma .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-8pLghYUGT1rUbUma .dashed-line{stroke-dasharray:3}#mermaid-svg-8pLghYUGT1rUbUma #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-8pLghYUGT1rUbUma #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-8pLghYUGT1rUbUma #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-8pLghYUGT1rUbUma #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-8pLghYUGT1rUbUma #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-8pLghYUGT1rUbUma #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-8pLghYUGT1rUbUma #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-8pLghYUGT1rUbUma #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-8pLghYUGT1rUbUma .commit-id,#mermaid-svg-8pLghYUGT1rUbUma .commit-msg,#mermaid-svg-8pLghYUGT1rUbUma .branch-label{fill:lightgrey;color:lightgrey;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-8pLghYUGT1rUbUma .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-8pLghYUGT1rUbUma .slice{font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-8pLghYUGT1rUbUma g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-8pLghYUGT1rUbUma g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-8pLghYUGT1rUbUma g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-8pLghYUGT1rUbUma g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-8pLghYUGT1rUbUma g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-8pLghYUGT1rUbUma g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-8pLghYUGT1rUbUma .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-8pLghYUGT1rUbUma .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-8pLghYUGT1rUbUma .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-8pLghYUGT1rUbUma .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-8pLghYUGT1rUbUma .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-8pLghYUGT1rUbUma .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-8pLghYUGT1rUbUma .edgeLabel text{fill:#333}#mermaid-svg-8pLghYUGT1rUbUma .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:‘trebuchet ms’, verdana, arial;font-family:var(–mermaid-font-family)}#mermaid-svg-8pLghYUGT1rUbUma .node circle.state-start{fill:black;stroke:black}#mermaid-svg-8pLghYUGT1rUbUma .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-8pLghYUGT1rUbUma #statediagram-barbEnd{fill:#9370db}#mermaid-svg-8pLghYUGT1rUbUma .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-8pLghYUGT1rUbUma .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-8pLghYUGT1rUbUma .statediagram-state .divider{stroke:#9370db}#mermaid-svg-8pLghYUGT1rUbUma .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-8pLghYUGT1rUbUma .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-8pLghYUGT1rUbUma .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-8pLghYUGT1rUbUma .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-8pLghYUGT1rUbUma .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-8pLghYUGT1rUbUma .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-8pLghYUGT1rUbUma .note-edge{stroke-dasharray:5}#mermaid-svg-8pLghYUGT1rUbUma .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{–mermaid-font-family: ‘“trebuchet ms”, verdana, arial’;–mermaid-font-family: “Comic Sans MS”, “Comic Sans”, cursive}#mermaid-svg-8pLghYUGT1rUbUma .error-icon{fill:#522}#mermaid-svg-8pLghYUGT1rUbUma .error-text{fill:#522;stroke:#522}#mermaid-svg-8pLghYUGT1rUbUma .edge-thickness-normal{stroke-width:2px}#mermaid-svg-8pLghYUGT1rUbUma .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-8pLghYUGT1rUbUma .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-8pLghYUGT1rUbUma .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-8pLghYUGT1rUbUma .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-8pLghYUGT1rUbUma .marker{fill:#333}#mermaid-svg-8pLghYUGT1rUbUma .marker.cross{stroke:#333} :root { --mermaid-font-family: “trebuchet ms”, verdana, arial;} #mermaid-svg-8pLghYUGT1rUbUma { color: rgba(0, 0, 0, 0.75); font: ; }

数据源

filter

map

终止操作

注意:

  1. Stream 自已不会存储元素

  2. Stream 不会改变源对象。相反,他们会返回一个持有结果的新Stream

  3. Stream 操作是延迟执行的。这意味着他们会等到需要结果的时候才执行。

案例(围绕以上三点进行操作)

首先我们先来了解两个概念

并行流(parallelStream):多个线程同时运行

顺序流(stream):使用主线程,单线程

  • Stream 实例化

创建 Stream 方式一:通过集合创建

public class LambdaTest {

@Test

public void test(){

List students= StudentData.getStudent();

//返回一个顺序流

Stream stream = students.stream();

//返回一个并行流

Stream parallelStream = students.parallelStream();

}

}

创建 Stream 方式二:通过数组创建(Java8中的Arrays的静态方法 Stream()可以获取数组流)

public class LambdaTest {

@Test

public void test(){

int[] arr =new int[]{1,2,3,4,5};

//通过泛型识明你的类型 放进去的是一个int类型的数组,返回的也是int类型的流

IntStream stream = Arrays.stream(arr);

}

}

创建 Stream 方式三:通过Stream的of()

public class LambdaTest {

@Test

public void test(){

Stream integerStream = Stream.of(1, 2, 3, 4, 5, 6);

}

}

创建 Stream方式四:创建无限流 (了解)

public class LambdaTest {

@Test

public void test(){

/**

  • 例子:遍历前10个偶数并打印出来

  • iterate(): 迭代

  • seed:种子

  • UnaryOperator:函数式接口

  • 注意:无限流不加对应的中间操作会无限的进行迭代 limit(10):前10个

*/

Stream.iterate(0, t -> t+2).limit(10).forEach(System.out::println);

}

}

  • Stream 中间操作

多个中间操作可以连接起来形成一个 “流水线” ,除非流水线上触发终止操作,否则中间操作不会执行任何的处理!而在终止操作时一次全部处理,成为 “惰性求值”。

| 筛选与切片方法 | 描述 |

| — | — |

| filter(Predicate p) | 接收Lambda,从流中排除某些元素 |

| distinct() | 筛选,通过流所产生元素的 hashCode() 和 equals() 去除重复元素 |

| limit(long maxSize) | 截断流,使其元素不超过给定数量 |

| skip(long n) | 跳过元素,返回一个扔掉了前 n 个元素的流,若流中元素不足 n 个,则返回一个空流。与 limit(n) 互补 |

| | |

| 映射方法 | 描述 |

| map(Funcation f) | 接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素 |

| mapToDouble(ToDoubleFunction f) | 接收一个函数作为参数,该函数会被应用到每个元素上,产生一个新的 DoubleStream |

| mapToInt(TolintFunction f) | 接收一个函数作为参数,该函数会被应用到每个元素上,产生一个新的IntStream |

| mapToLong(ToLongFunction f) | 接收一个函数作为参数,该函数会被应用到每个元素上,产生一个新的LongStream |

| flatMap(Function f) | 接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流 |

| | |

| 排序方法 | 描述 |

| sorted() | 产生一个新流,其中按自然顺序排序 |

| sorted(Comparator com) | 产生一个新流,其中按比较器顺序排序 |

filter 用法

public class LambdaTest {

@Test

public void test(){

List students = StudentData.getStudent();

//筛选出性别为男的同学信息

students.stream().filter(p-> ‘男’ == p.getsex()).forEach(System.out::println);

}

}

在这里插入图片描述

distinct 用法 (去重复数据是通过流所生成元素的 hashCode()equals() 所以实体类中需要生成对应的方法)

public class LambdaTest {

@Test

public void test(){

List students = StudentData.getStudent();

//去掉重复的数据

students.stream().distinct().forEach(System.out::println);

}

}

在这里插入图片描述

limt 用法

public class LambdaTest {

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

public class LambdaTest {

@Test

public void test(){

List students = StudentData.getStudent();

//筛选出性别为男的同学信息

students.stream().filter(p-> ‘男’ == p.getsex()).forEach(System.out::println);

}

}

在这里插入图片描述

distinct 用法 (去重复数据是通过流所生成元素的 hashCode()equals() 所以实体类中需要生成对应的方法)

public class LambdaTest {

@Test

public void test(){

List students = StudentData.getStudent();

//去掉重复的数据

students.stream().distinct().forEach(System.out::println);

}

}

在这里插入图片描述

limt 用法

public class LambdaTest {

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-gKNsxMhx-1715483434714)]

[外链图片转存中…(img-k3qWfW0F-1715483434715)]

[外链图片转存中…(img-cdFyvfOe-1715483434715)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值