既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
二.函数式(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<Double> consumer){
consumer.accept(money);
}
@Test
public void test(){
//原写法
happyTime(500, new Consumer<Double>() {
@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<String> supplier=new Supplier<String>() {
@Override
public String get() {
return "原写法,供给型接口";
}
};
System.out.println(supplier.get());
//Lambda表达式写法
Supplier<String> 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<String> filterString(List<String> list, Predicate<String> predicate){
List<String> filterList=new ArrayList<>();
for (String s:list){
if (predicate.test(s)){
filterList.add(s);
}
}
return filterList;
}
@Test
public void test(){
//原写法
List<String> list=Arrays.asList(new String[]{"上海", "香港", "澳门", "曹县"});
List<String> filterString = filterString(list, new Predicate<String>() {
@Override
public boolean test(String s) {
return "曹县".equals(s) ? true : false;
}
});
System.out.println(filterString);
//Lambda表达式写法
List<String> lfilterString = filterString(list, s -> "上海".equals(s) ? true : false);
System.out.println(lfilterString);
}
}
三.方法引用与构造器引用
方法引用
当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用 ! 方法引用可以看作是Lambda表达式深层次的表达,换句话说,
方法引用就是Lambda表达式,也就是函数式接口的一个实例
,通过方法名字来指向一个方法,可以认为是Lambda表达式的一个语法糖。
语法:
使用操作符 “::” 将类(或对象)与方法名分割开来。
要求:实现接口的抽象方法的参数和返回值类型,必须与方法引用的方法的参数列表和返回值类型保持一致!
案例:
- 格式:对象 : : 非静态方法
@Test
public void test(){
//Lambda表达式写法
Consumer<String> consumer = str ->System.out.println(str);
consumer.accept("Lambda表达式");
//方法引用写法
Consumer<String> fConsumer = System.out::println;
fConsumer.accept("方法引用");
}
- 格式:类 : : 静态方法
public class LambdaTest {
@Test
public void test(){
//Lambda表达式写法 比较两个数大小
Comparator<Integer> comparator =(t1,t2) -> Integer.compare(t1,t2);
System.out.println(comparator.compare(10,20));
//方法引用写法
Comparator<Integer> fComparator = Integer::compare;
System.out.println(fComparator.compare(20,10));
}
}
- 格式:类 : : 非静态方法
public class LambdaTest {
@Test
public void test(){
//Lambda表达式写法
Comparator<String> comparator=(s1,s2) -> s1.compareTo(s2);
System.out.println(comparator.compare("abc","abd"));
//方法引用写法
Comparator<String> 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 +
'}';
}
}
- 通过无参构造器引用(创建对象)
public class LambdaTest {
@Test
public void test(){
//原始写法
Supplier<Student> supplier =new Supplier<Student>() {
@Override
public Student get() {
return new Student();
}
};
supplier.get();
//lambda表达式写法
Supplier<Student> lSupplier =()-> new Student();
lSupplier.get();
//构造器引用写法
Supplier<Student> gSupplier = Student::new;
gSupplier.get();
}
}
- 通过有参构造器引用(一个参数,创建对象)
@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 操作的三个步骤
创建 Stream
一个数据源(集合,数组),获取一个流中间操作
一个中间操作链,对数据源的数据进行处理终止操作(终端操作)
一旦执行终止操作,就执行中间操作链,并产生结果。之后,不会再被使用
#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
…
终止操作
注意:
- Stream 自已不会存储元素
- Stream 不会改变源对象。相反,他们会返回一个持有结果的新Stream
- Stream 操作是延迟执行的。这意味着他们会等到需要结果的时候才执行。
案例(围绕以上三点进行操作)
首先我们先来了解两个概念
并行流(parallelStream):多个线程同时运行
顺序流(stream):使用主线程,单线程
- Stream 实例化
创建 Stream 方式一:通过集合创建
public class LambdaTest {
@Test
public void test(){
List<Student> students= StudentData.getStudent();
//返回一个顺序流
Stream<Student> stream = students.stream();
//返回一个并行流
Stream<Student> 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<Integer> 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<Student> students = StudentData.getStudent();
//筛选出性别为男的同学信息
students.stream().filter(p-> '男' == p.getsex()).forEach(System.out::println);
}
}
distinct 用法 (去重复数据是通过流所生成元素的
hashCode()
和equals()
所以实体类中需要生成对应的方法)
public class LambdaTest {
@Test
public void test(){
List<Student> students = StudentData.getStudent();
//去掉重复的数据
students.stream().distinct().forEach(System.out::println);
}
}
limt 用法
public class LambdaTest {
@Test
public void test(){
List<Student> students = StudentData.getStudent();
//展示前三个同学的信息
students.stream().limit(3).forEach(System.out::println);
}
}
skip 用法
public class LambdaTest {
@Test
public void test(){
List<Student> students = StudentData.getStudent();
//显示除了前三个之外的学生信息
students.stream().skip(3).forEach(System.out::println);
}
}
map 用法
public class LambdaTest {
@Test
public void test(){
List<Student> students = StudentData.getStudent();
//获取到男学生的姓名映射到新的集合中并打印
students.stream().filter(student -> '男'==student.getsex()).map(Student::getName).forEach(System.out::println);
}
}
- Stream 终止操作
终止操作 (终端操作) 从流水线生成结果。其结果可以是任何不是流的值,例如:List,Integer,甚至是 void。
流进行了终止操作后,不能再使用!
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
M6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTY5MjcwNQ==,size_16,color_FFFFFF,t_70)
map 用法
public class LambdaTest {
@Test
public void test(){
List<Student> students = StudentData.getStudent();
//获取到男学生的姓名映射到新的集合中并打印
students.stream().filter(student -> '男'==student.getsex()).map(Student::getName).forEach(System.out::println);
}
}
- Stream 终止操作
终止操作 (终端操作) 从流水线生成结果。其结果可以是任何不是流的值,例如:List,Integer,甚至是 void。
流进行了终止操作后,不能再使用!
[外链图片转存中…(img-Txc5cUFH-1715765074326)]
[外链图片转存中…(img-NdpmJI11-1715765074326)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!