概述
为什么学?
- 能够看懂公司里的代码
- 大数量下处理集合效率高
- 代码可读性高
- 消灭嵌套地狱
//查询未成年作家的评分在70以上的书籍 由于洋流影响所以作家和书籍可能出现重复,需要进行去重
List<Book> bookList = new ArrayList<>();//用来存放结果
Set<Book> uniqueBookValues = new HashSet<>();//作用是用来去重,如果往里面添加成功就说明之前没有遇到过
Set<Author> uniqueAuthorValues = new HashSet<>();//作用是用来去重,如果往里面添加成功就说明之前没有遇到过
for (Author author : authors) {//遍历作家
if (uniqueAuthorValues.add(author)) {//对作家去重
if (author.getAge() < 18) {
List<Book> books = author.getBooks();
for (Book book : books) {//遍历书籍
if (book.getScore() > 70) {//70分以上
if (uniqueBookValues.add(book)) {//对书籍去重
bookList.add(book);//收入结果集
}
}
}
}
}
}
System.out.println(bookList);
如果使用函数式编程,那么就像下面一样简单,代码具体逻辑等讲完再来看:
List<Book> collect = authors.stream()
.distinct()
.filter(author -> author.getAge() < 18)
.map(author -> author.getBooks())
.flatMap(Collection::stream)
.filter(book -> book.getScore() > 70)
.distinct()
.collect(Collectors.toList());
System.out.println(collect);
函数式编程思想
概念
面向对象思想需要关注用什么对象完成什么事情。而函数式编程思想就类似于我们数学中的函数。它主要关注的是对数据进行了什么操作。
优点
- 代码简洁,开发快速
- 接近自然语言,易于理解
- 易于"并发编程"
Lambda表达式
概述
Lambda是JDK8中一个语法糖。他可以对某些匿名内部类的写法进行简化。它是函数式编程思想的一个重要体现。让我们不用关注是什么对象。而是更关注我们对数据进行了什么操作。
核心原则
可推导可省略
基本格式
(参数列表)->{代码}
例一
我们在创建线程并启动时可以使用匿名内部类的写法:
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("hello");
}
}).start();
可以使用Lambda的格式对其进行修改。修改后如下:
new Thread(()->{
System.out.println("hello");
}).start();
例二
现有方法定义如下,其中IntBinaryOperator是一个接口。先使用匿名内部类的写法调用该方法。
public static int calculateNum(IntBinaryOperator operator){
int a = 10;
int b = 20;
return operator.applyAsInt(a, b);
}
public static void main(String[] args) {
int i = calculateNum(new IntBinaryOperator() {
@Override
public int applyAsInt(int left, int right) {
return left + right;
}
});
System.out.println(i);//30
}
Lambda写法:
public static int calculateNum(IntBinaryOperator operator){
int a = 10;
int b = 20;
return operator.applyAsInt(a, b);
}
public static void main(String[] args) {
int i = calculateNum((int left, int right)->{
return left + right;
});
System.out.println(i);
}
例三
现有方法定义如下,其中IntPredicate是一个接口。先使用匿名内部类的写法调用该方法。
public static void printNum(IntPredicate predicate){
int[] arr = {1,2,3,4,5,6,7,8,9,10};
for (int i : arr) {
if(predicate.test(i)){
System.out.println(i);
}
}
}
public static void main(String[] args) {
printNum(new IntPredicate() {
@Override
public boolean test(int value) {
return value%2==0;
}
});
}
Lambda写法:
public static void main(String[] args) {
printNum((int value)-> {
return value%2==0;
});
}
public static void printNum(IntPredicate predicate){
int[] arr = {1,2,3,4,5,6,7,8,9,10};
for (int i : arr) {
if(predicate.test(i)){
System.out.println(i);
}
}
}
例四
现有方法定义如下,其中Function是一个接口。先使用匿名内部类的写法调用该方法。
//这个方法的逻辑就是我要依靠Function去完成将字符串转换成别的类型,但是具体转换成什么类型我还不确定,希望调用这个方法的人自己去决定(使用泛型和接口的好处就在于没有把代码写死,便于代码扩展,写代码变得更加灵活,自由度更高)
public static <R> R typeConver(Function<String,R> function){
String str = "1235";
R result = function.apply(str);
return result;
}
public static void main(String[] args) {
Integer result = typeConver(new Function<String, Integer>() {
@Override
public Integer apply(String s) {
return Integer.valueOf(s);
}
});
System.out.println(result);
}
Lambda写法:
Integer result = typeConver((String s)->{
return Integer.valueOf(s);
});
System.out.println(result);
时刻记住:Lambda不关心对象而是关心对数据进行了什么操作,其实就很像面向过程编程。
例五
现有方法定义如下,其中IntConsumer是一个接口。先使用匿名内部类的写法调用该方法。
public static void foreachArr(IntConsumer consumer){
int[] arr = {1,2,3,4,5,6,7,8,9,10};
for (int i : arr) {
consumer.accept(i);
}
}
public static void main(String[] args) {
foreachArr(new IntConsumer() {
@Override
public void accept(int value) {
System.out.println(value);
}
});
}
Lambda写法:
public static void main(String[] args) {
foreachArr((int value)->{
System.out.println(value);
});
}
方法里传入一个消费者,用来消费数据,具体怎么消费由调用该方法的人决定,决定方法就是重写接口的方法
省略规则
- 参数类型可以省略(要么全部省略,要么都不省略)
- 方法体只有一句代码时大括号、return和唯一一句代码的分号可以省略
- 方法只有一个参数时小括号可以省略
- 以上这些规则都记不住也可以省略不记(不用刻意记,用多了就自然记住了)