最后
小编在这里分享些我自己平时的学习资料,由于篇幅限制,pdf文档的详解资料太全面,细节内容实在太多啦,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!
开源分享:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】
程序员代码面试指南 IT名企算法与数据结构题目最优解
这是” 本程序员面试宝典!书中对IT名企代码面试各类题目的最优解进行了总结,并提供了相关代码实现。针对当前程序员面试缺乏权威题目汇总这一-痛点, 本书选取将近200道真实出现过的经典代码面试题,帮助广“大程序员的面试准备做到万无一失。 “刷”完本书后,你就是“题王”!
《TCP-IP协议组(第4版)》
本书是介绍TCP/IP协议族的经典图书的最新版本。本书自第1版出版以来,就广受读者欢迎。
本书最新版进行」护元,以体境计算机网络技不的最新发展,全书古有七大部分共30草和7个附录:第一部分介绍一些基本概念和基础底层技术:第二部分介绍网络层协议:第三部分介绍运输层协议;第四部分介绍应用层协议:第五部分介绍下一代协议,即IPv6协议:第六部分介绍网络安全问题:第七部分给出了7个附录。
Java开发手册(嵩山版)
这个不用多说了,阿里的开发手册,每次更新我都会看,这是8月初最新更新的**(嵩山版)**
MySQL 8从入门到精通
本书主要内容包括MySQL的安装与配置、数据库的创建、数据表的创建、数据类型和运算符、MySQL 函数、查询数据、数据表的操作(插入、更新与删除数据)、索引、存储过程和函数、视图、触发器、用户管理、数据备份与还原、MySQL 日志、性能优化、MySQL Repl ication、MySQL Workbench、 MySQL Utilities、 MySQL Proxy、PHP操作MySQL数据库和PDO数据库抽象类库等。最后通过3个综合案例的数据库设计,进步讲述 MySQL在实际工作中的应用。
Spring5高级编程(第5版)
本书涵盖Spring 5的所有内容,如果想要充分利用这一领先的企业级 Java应用程序开发框架的强大功能,本书是最全面的Spring参考和实用指南。
本书第5版涵盖核心的Spring及其与其他领先的Java技术(比如Hibemate JPA 2.Tls、Thymeleaf和WebSocket)的集成。本书的重点是介绍如何使用Java配置类、lambda 表达式、Spring Boot以及反应式编程。同时,将与企业级应用程序开发人员分享一些见解和实际经验,包括远程处理、事务、Web 和表示层,等等。
JAVA核心知识点+1000道 互联网Java工程师面试题
企业IT架构转型之道 阿里巴巴中台战略思想与架构实战
本书讲述了阿里巴巴的技术发展史,同时也是-部互联网技 术架构的实践与发展史。
- 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表达式的一个语法糖。
语法:
使用操作符 “::” 将类(或对象)与方法名分割开来。
要求:实现接口的抽象方法的参数和返回值类型,必须与方法引用的方法的参数列表和返回值类型保持一致!
案例:
- 格式:对象 : : 非静态方法
@Test
public void test(){
//Lambda表达式写法
Consumer consumer = str ->System.out.println(str);
consumer.accept(“Lambda表达式”);
//方法引用写法
Consumer fConsumer = System.out::println;
fConsumer.accept(“方法引用”);
}
- 格式:类 : : 静态方法
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));
}
}
- 格式:类 : : 非静态方法
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 +
‘}’;
}
}
- 通过无参构造器引用(创建对象)
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();
}
}
- 通过有参构造器引用(一个参数,创建对象)
@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)));
}
}
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 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);
}
}
public class LambdaTest {
@Test
public void test(){
List students=StudentData.getStudent();
//学生id的总和
Optional idSum = students.stream().map(Student::getId).reduce(Integer::sum);
System.out.println(idSum);
}
}
collect 方法 (Collector 对象也有很多可选方法)
public class LambdaTest {
@Test
public void test(){
List students=StudentData.getStudent();
//查找性别为女的学生并返回一个 list集合
List list = students.stream().filter(student -> ‘女’ == student.getsex()).collect(Collectors.toList());
System.out.println(list);
}
}
在此只演示个别的方法,小伙伴们私下可自行练习 (^ - ^) !
Optional<T>
类可以优雅的处理空指针异常。它可以保存T的值,代表这个值存在。或者仅仅保存null,表示这个值不存在。原来用null表示一个值不存在,现在Optional
可以更好的表达这个概念。并且可以避免空指针异常。
Optional提供了很多方法,这样我们就不用显式进行空值检测。
【创建实例对象】
| 创建Optional类对象
方法 | 描述 |
| — | — |
| of(T t) | 创建一个Optional实例 t 必须非空 |
| empty() | 创建一个空的 Optional 实例 |
| ofNullable(T t) | t 可以为 null |
通过 of() 方法创建对象,school 不能为空,为空则抛出控制值异常
public class LambdaTest {
@Test
public void test() {
School school=new School();
Optional optionalSchool = Optional.of(school);
}
}
public class LambdaTest {
@Test
public void test() {
School school=new School();
school=null;
Optional optionalSchool = Optional.of(school);
}
}
通过 ofNullable(school) 创建对象,school 可以为空
public class LambdaTest {
@Test
public void test() {
School school=new School();
school=null;
Optional optionalSchool = Optional.ofNullable(school);
System.out.println(optionalSchool);
}
}
【对容器中对象进行操作】
| 判断Optional容器中是否包含对象
方法 | 描述 |
| — | — |
| boolean isPresent() | 判断是否包含对象 |
| boolean ifPresent(Consumer<? super T> consumer) | 如果有值,就执行Consumer接口的实现代码,并且该值会作为参数传给它 |
Optional对象的 isPresent() 方法使用
public class LambdaTest {
@Test
public void test() {
School school=new School();
school=null;
Optional optionalSchool = Optional.ofNullable(school);
最后
作为过来人,小编是整理了很多进阶架构视频资料、面试文档以及PDF的学习资料,针对上面一套系统大纲小编也有对应的相关进阶架构视频资料
ol=new School();
school=null;
Optional optionalSchool = Optional.of(school);
}
}
通过 ofNullable(school) 创建对象,school 可以为空
public class LambdaTest {
@Test
public void test() {
School school=new School();
school=null;
Optional optionalSchool = Optional.ofNullable(school);
System.out.println(optionalSchool);
}
}
【对容器中对象进行操作】
| 判断Optional容器中是否包含对象
方法 | 描述 |
| — | — |
| boolean isPresent() | 判断是否包含对象 |
| boolean ifPresent(Consumer<? super T> consumer) | 如果有值,就执行Consumer接口的实现代码,并且该值会作为参数传给它 |
Optional对象的 isPresent() 方法使用
public class LambdaTest {
@Test
public void test() {
School school=new School();
school=null;
Optional optionalSchool = Optional.ofNullable(school);
最后
作为过来人,小编是整理了很多进阶架构视频资料、面试文档以及PDF的学习资料,针对上面一套系统大纲小编也有对应的相关进阶架构视频资料
[外链图片转存中…(img-lBLNpR56-1715455764043)]
[外链图片转存中…(img-ZQVxouF6-1715455764043)]