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

| 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 {

@Test

public void test(){

List students = StudentData.getStudent();

//展示前三个同学的信息

students.stream().limit(3).forEach(System.out::println);

}

}

在这里插入图片描述

skip 用法

public class LambdaTest {

@Test

public void test(){

List students = StudentData.getStudent();

//显示除了前三个之外的学生信息

students.stream().skip(3).forEach(System.out::println);

}

}

在这里插入图片描述

map 用法

public class LambdaTest {

@Test

public void test(){

List students = StudentData.getStudent();

//获取到男学生的姓名映射到新的集合中并打印

students.stream().filter(student -> ‘男’==student.getsex()).map(Student::getName).forEach(System.out::println);

}

}

在这里插入图片描述

  • Stream 终止操作

终止操作 (终端操作) 从流水线生成结果。其结果可以是任何不是流的值,例如:List,Integer,甚至是 void。

流进行了终止操作后,不能再使用!

| 匹配与查找方法 | 描述 |

| — | — |

| allMatch(Predicate p) | 检查是否匹配所有元素 |

| anyMatch(Predicate p) | 检查是否至少匹配一个元素 |

| noneMatch(Predicate p) | 检查是否没有匹配所有元素 |

| findFirst() | 返回第一个元素 |

| findAny() | 返回当前流中的任意元素 |

| count() | 返回流中元素的总个数 |

| max(Comparator c) | 返回流中最大值 |

| min(Comparator c) | 返回流中最小值 |

| forEach(Consumer c) | 内部迭代 |

| | |

| 规约 方法 | 描述 |

| reduce(T iden,BlnaryOperator b) | 可以将流中元素反复结合起来,得到一个值。返回 T |

| reduce(BlnaryOperator b) | 可以将流中元素反复结合起来,得到一个值。返回 Optional < T > |

| | |

| 收集 方法 | 描述 |

| collect(Collector c) | 将流转换为其他形式。接收一个 Collector接口的实现,用于给Stream中元素做汇总的方法 |

allMatch 方法 (allMatch方法需要所有匹配才会返回true)

public class LambdaTest {

@Test

public void test(){

List students = StudentData.getStudent();

//所有学生是不是都是男生

boolean rst = students.stream().allMatch(student -> ‘男’ == student.getsex());

System.out.println(rst);

}

}

在这里插入图片描述

anyMatch 方法 (anyMatch方法只需要一个元素匹配就可以返回true)

public class LambdaTest {

@Test

public void test(){

List students = StudentData.getStudent();

//是否存在人妖

boolean rst = students.stream().anyMatch(student -> ‘妖’ == student.getsex());

System.out.println(rst);

}

}

在这里插入图片描述

reduce 方法

public class LambdaTest {

@Test

public void test(){

List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

//计算 1-10 的自然数的和

Integer sum = list.stream().reduce(0, Integer::sum);

System.out.println(sum);

}

}

总结

以上是字节二面的一些问题,面完之后其实挺后悔的,没有提前把各个知识点都复习到位。现在重新好好复习手上的面试大全资料(含JAVA、MySQL、算法、Redis、JVM、架构、中间件、RabbitMQ、设计模式、Spring等),现在起闭关修炼半个月,争取早日上岸!!!

下面给大家分享下我的面试大全资料

  • 第一份是我的后端JAVA面试大全

image.png

后端JAVA面试大全

  • 第二份是MySQL+Redis学习笔记+算法+JVM+JAVA核心知识整理

字节二面拜倒在“数据库”脚下,闭关修炼半个月,我还有机会吗?

MySQL+Redis学习笔记算法+JVM+JAVA核心知识整理

  • 第三份是Spring全家桶资料

字节二面拜倒在“数据库”脚下,闭关修炼半个月,我还有机会吗?

MySQL+Redis学习笔记算法+JVM+JAVA核心知识整理

{

@Test

public void test(){

List students = StudentData.getStudent();

//所有学生是不是都是男生

boolean rst = students.stream().allMatch(student -> ‘男’ == student.getsex());

System.out.println(rst);

}

}

在这里插入图片描述

anyMatch 方法 (anyMatch方法只需要一个元素匹配就可以返回true)

public class LambdaTest {

@Test

public void test(){

List students = StudentData.getStudent();

//是否存在人妖

boolean rst = students.stream().anyMatch(student -> ‘妖’ == student.getsex());

System.out.println(rst);

}

}

在这里插入图片描述

reduce 方法

public class LambdaTest {

@Test

public void test(){

List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

//计算 1-10 的自然数的和

Integer sum = list.stream().reduce(0, Integer::sum);

System.out.println(sum);

}

}

总结

以上是字节二面的一些问题,面完之后其实挺后悔的,没有提前把各个知识点都复习到位。现在重新好好复习手上的面试大全资料(含JAVA、MySQL、算法、Redis、JVM、架构、中间件、RabbitMQ、设计模式、Spring等),现在起闭关修炼半个月,争取早日上岸!!!

下面给大家分享下我的面试大全资料

  • 第一份是我的后端JAVA面试大全

[外链图片转存中…(img-9fXLGim7-1714406671852)]

后端JAVA面试大全

  • 第二份是MySQL+Redis学习笔记+算法+JVM+JAVA核心知识整理

[外链图片转存中…(img-9nqUyYyn-1714406671852)]

MySQL+Redis学习笔记算法+JVM+JAVA核心知识整理

  • 第三份是Spring全家桶资料

[外链图片转存中…(img-M0kDqYqK-1714406671852)]

MySQL+Redis学习笔记算法+JVM+JAVA核心知识整理

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值