Lambda表达式(三更草堂)
Posted Scean周
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lambda表达式(三更草堂)相关的知识,希望对你有一定的参考价值。
一、函数式编程-Stream流
1、概述
1.1 为什么学?
1.2 函数式编程思想
1.2.1 概念
1.2.2 优点
2、Lambda表达式
2.1 概述
2.2 核心原则
可推导可省略
2.3 基本格式
(参数列表)->代码
2.3.1 例一
public static void main(String[] args)
//优化前
new Thread(new Runnable()
@Override
public void run()
System.out.println("新线程中run方法被执行了!");
).start();
//优化后 (参数列表)->代码
new Thread(() ->
System.out.println("新线程中run方法被执行了!");
).start();
//进一步优化
new Thread(() -> System.out.println("新线程中run方法被执行了!")).start();
2.3.2 例二
public static void main(String[] args)
//优化前
int result1 = calculateNum(new IntBinaryOperator()
@Override
public int applyAsInt(int left, int right)
return left + right;
);
System.out.println(result1);
//优化后
int result2 = calculateNum((int left, int right) ->
return left + right;
);
System.out.println(result2);
//进一步优化
int result3 = calculateNum((left, right) -> left + right);
System.out.println(result3);
public static int calculateNum(IntBinaryOperator operator)
int a = 10;
int b = 20;
return operator.applyAsInt(a, b);
2.3.3 例三
public static void main(String[] args)
//优化前
printNum(new IntPredicate()
@Override
public boolean test(int value)
return value % 2 == 0;
);
//优化后
printNum((int value) ->
return value % 2 == 0;
);
//进一步优化
printNum(value -> 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);
2.3.4 例四
public static void main(String[] args)
//优化前
Integer result1 = typeConver(new Function<String, Integer>()
@Override
public Integer apply(String s)
return Integer.valueOf(s);
);
System.out.println(result1);//12345
//优化后
Integer result2 = typeConver((String s) ->
return Integer.valueOf(s);
);
System.out.println(result2);//12345
//进一步优化
Integer result3 = typeConver(s -> Integer.valueOf(s));
System.out.println(result3);//12345
public static <R> R typeConver(Function<String, R> function)
String str = "12345";
R result = function.apply(str);
return result;
2.3.5 例五
//优化前
foreachArr(new IntConsumer()
@Override
public void accept(int value)
System.out.println(value);
);
//优化后
foreachArr((int value) ->
System.out.println(value);
);
//进一步优化
foreachArr(value -> System.out.println(value));
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);
2.4 省略规则
3、Stream流
3.1 概述
3.2 案例数据准备
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode//用于后期的去重使用
public class Author
//id
private Long id;
//姓名
private String name;
//年龄
private Integer age;
//简介
private String intro;
//作品
private List<Book> books;
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode//用于后期的去重使用
public class Book
//id
private Long id;
//书名
private String name;
//分类
private String category;//"哲学,小说"
//评分
private Integer score;
//简介
private String intro;
private static List<Author> getAuthors()
//数据初始化
Author author = new Author(1L, "蒙多", 33, "一个从菜刀中明悟哲理的祖安人", null);
Author author2 = new Author(2L, "亚拉索", 15, "狂风也追逐不上他的思考速度", null);
Author author3 = new Author(3L, "易", 14, "是这个世界在限制他的思维", null);
Author author4 = new Author(3L, "易", 14, "是这个世界在限制他的思维", null);
//书籍列表
List<Book> books1 = new ArrayList<>();
List<Book> books2 = new ArrayList<>();
List<Book> books3 = new ArrayList<>();
books1.add(new Book(1L, "刀的两侧是光明与黑暗", "哲学,爱情", 88, "用一把刀划分了爱恨"));
books1.add(new Book(2L, "一个人不能死在同一把刀下", "个人成长,爱情", 99, "讲述如何从失败中明悟真理"));
books2.add(new Book(3L, "那风吹不到的地方", "哲学", 85, "带你用思维去领略世界的尽头"));
books2.add(new Book(3L, "那风吹不到的地方", "哲学", 85, "带你用思维去领略世界的尽头"));
books2.add(new Book(4L, "吹或不吹", "爱情,个人传记", 56, "一个哲学家的恋爱观注定很难把他所在的时代理解"));
books3.add(new Book(5L, "你的剑就是我的剑", "爱情", 56, "无法想象一个武者能对他的伴侣这么的宽容"));
books3.add(new Book(6L, "风与剑", "个人传记", 100, "两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢?"));
books3.add(new Book(6L, "风与剑", "个人传记", 100, "两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢?"));
author.setBooks(books1);
author2.setBooks(books2);
author3.setBooks(books3);
author4.setBooks(books3);
List<Author> authorList = new ArrayList<>(Arrays.asList(author, author2, author3, author4));
return authorList;
3.3 快速入门
3.3.1 需求
3.3.2 实现
//优化前
private static void test01()
List<Author> authors = getAuthors();
Stream<Author> stream = authors.stream();
//把集合转换成流
stream
//去重
.distinct()
//过滤条件
.filter(new Predicate<Author>()
@Override
public boolean test(Author author)
return author.getAge() < 18;
)
//打印
.forEach(new Consumer<Author>()
@Override
public void accept(Author author)
System.out.println(author.getName() + "今年" + author.getAge() + "岁");
);
//优化后
private static void test01()
List<Author> authors = getAuthors();
//把集合转换成流
authors.stream()
//去重
.distinct()
//过滤条件
.filter(author -> author.getAge() < 18)
//打印
.forEach(author -> System.out.println(author.getName() + "今年" + author.getAge() + "岁"));
3.4 常用操作
3.4.1 创建流
① 单列集合
集合对象.stream( )
private static void test01()
List<Author> authors = getAuthors();
Stream<Author> stream = authors.stream();
//把集合转换成流
stream
//去重
.distinct()
//过滤条件
.filter(author -> author.getAge() < 18)
//打印
.forEach(author -> System.out.println(author.getName() + "今年" + author.getAge() + "岁"));
② 数组
Arrays.stream(数组)或者使用Stream.of
来创建
private static void test02()
Integer[] arr = 1, 2, 3, 4, 5;
Stream<Integer> stream1 = Arrays.stream(arr);
stream1.distinct()
.filter(integer -> integer > 2)
.forEach(integer -> System.out.println(integer));
Stream<Integer> stream2 = Stream.of(arr);
stream2.distinct()
.filter(integer -> integer < 2)
.forEach(integer -> System.out.println(integer));
③ 双列集合
转换成单列集合后再创建
private static void test03()
Map<String, Integer> map = new HashMap<>();
map.put("蜡笔小新", 5);
map.put("zyy", 22);
map.put("zqh", 23);
Stream<Map.Entry<String, Integer>> stream = map.entrySet().stream();
stream.distinct()
.filter(entry -> entry.getValueLambda表达式(三更草堂)
超详细Docker部署SpringBoot+Vue项目(三更博客项目部署)