From 200deeeecbe983853843e2552f70e19e001925bd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E8=9C=97=E7=89=9B?=
<1003271829@qq.com>
Date: Sat, 30 Dec 2023 11:38:29 +0800
Subject: [PATCH] =?UTF-8?q?=E6=8A=80=E6=9C=AF=E6=80=BB=E7=9B=91=E5=86=99?=
=?UTF-8?q?=E7=9A=84=E5=8D=81=E4=B8=AA=E6=96=B9=E6=B3=95=EF=BC=8C=E8=AE=A9?=
=?UTF-8?q?=E6=88=91=E7=B2=BE=E9=80=9A=E4=BA=86lambda=E8=A1=A8=E8=BE=BE?=
=?UTF-8?q?=E5=BC=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../lambdatest/pom.xml | 57 +++++++++++++++++++
.../CollectionToListOrSet.java | 29 ++++++++++
.../collectiontolist/TestCollection.java | 35 ++++++++++++
.../collectiontomap/CollectionToMap.java | 39 +++++++++++++
.../collectiontomap/TestCollectionToMap.java | 33 +++++++++++
.../java/org/example/entity/OrderItem.java | 12 ++++
.../java/org/example/listtoset/ListToSet.java | 20 +++++++
.../org/example/listtoset/TestListToSet.java | 31 ++++++++++
.../org/example/maptovalue/MapToValue.java | 42 ++++++++++++++
.../example/maptovalue/TestMapToValue.java | 32 +++++++++++
10 files changed, 330 insertions(+)
create mode 100644 技术总监写的十个方法,让我精通了lambda表达式/lambdatest/pom.xml
create mode 100644 技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/collectiontolist/CollectionToListOrSet.java
create mode 100644 技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/collectiontolist/TestCollection.java
create mode 100644 技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/collectiontomap/CollectionToMap.java
create mode 100644 技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/collectiontomap/TestCollectionToMap.java
create mode 100644 技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/entity/OrderItem.java
create mode 100644 技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/listtoset/ListToSet.java
create mode 100644 技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/listtoset/TestListToSet.java
create mode 100644 技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/maptovalue/MapToValue.java
create mode 100644 技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/maptovalue/TestMapToValue.java
diff --git a/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/pom.xml b/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/pom.xml
new file mode 100644
index 0000000..77af04b
--- /dev/null
+++ b/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/pom.xml
@@ -0,0 +1,57 @@
+
+
+ 4.0.0
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 2.2.2.RELEASE
+
+
+ org.example
+ lambdatest
+ 1.0-SNAPSHOT
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+ org.junit.vintage
+ junit-vintage-engine
+
+
+
+
+
+ com.google.guava
+ guava
+ 32.1.3-jre
+
+
+
+ org.projectlombok
+ lombok
+ provided
+
+
+
+ com.alibaba
+ fastjson
+ 1.2.60
+
+
+ junit
+ junit
+
+
+
+
\ No newline at end of file
diff --git a/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/collectiontolist/CollectionToListOrSet.java b/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/collectiontolist/CollectionToListOrSet.java
new file mode 100644
index 0000000..67c0e15
--- /dev/null
+++ b/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/collectiontolist/CollectionToListOrSet.java
@@ -0,0 +1,29 @@
+package org.example.collectiontolist;
+
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+public class CollectionToListOrSet {
+
+ public static List toList(Collection collection) {
+ if (collection == null) {
+ return new ArrayList<>();
+ }
+ if (collection instanceof List) {
+ return (List) collection;
+ }
+ return collection.stream().collect(Collectors.toList());
+ }
+
+ public static Set toSet(Collection collection) {
+ if (collection == null) {
+ return new HashSet<>();
+ }
+ if (collection instanceof Set) {
+ return (Set) collection;
+ }
+ return collection.stream().collect(Collectors.toSet());
+ }
+
+}
diff --git a/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/collectiontolist/TestCollection.java b/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/collectiontolist/TestCollection.java
new file mode 100644
index 0000000..a4fa6b1
--- /dev/null
+++ b/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/collectiontolist/TestCollection.java
@@ -0,0 +1,35 @@
+package org.example.collectiontolist;
+
+
+import com.google.common.collect.Lists;
+import org.example.entity.OrderItem;
+import org.junit.Test;
+
+import java.util.List;
+import java.util.Set;
+
+public class TestCollection {
+
+ //将集合 Collection 转化为 List
+ @Test
+ public void testToList() {
+ List orderItems = Lists.newArrayList(
+ new OrderItem(1, 5d, "手表"),
+ new OrderItem(2, 6d, "机器人"),
+ new OrderItem(3, 8d, "手机")
+ );
+ List list = CollectionToListOrSet.toList(orderItems);
+ }
+
+ //将集合 Collection 转化为 Set
+ @Test
+ public void testToSet() {
+ List orderItems = Lists.newArrayList(
+ new OrderItem(1, 5d, "手表"),
+ new OrderItem(2, 6d, "机器人"),
+ new OrderItem(3, 8d, "手机")
+ );
+ Set set = CollectionToListOrSet.toSet(orderItems);
+ }
+
+}
diff --git a/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/collectiontomap/CollectionToMap.java b/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/collectiontomap/CollectionToMap.java
new file mode 100644
index 0000000..a450fea
--- /dev/null
+++ b/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/collectiontomap/CollectionToMap.java
@@ -0,0 +1,39 @@
+package org.example.collectiontomap;
+
+import org.springframework.util.CollectionUtils;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.BinaryOperator;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+
+// Collection 转化为 Map
+public class CollectionToMap {
+ public static Map toMap(Collection collection, Function super T, ? extends K> keyMapper) {
+ return toMap(collection, keyMapper, Function.identity());
+ }
+
+ public static Map toMap(Collection collection, Function super T, ? extends K> keyFunction, Function super T, ? extends V> valueFunction) {
+ return toMap(collection, keyFunction, valueFunction, pickSecond());
+ }
+
+ public static Map toMap(Collection collection, Function super T, ? extends K> keyFunction, Function super T, ? extends V> valueFunction, BinaryOperator mergeFunction) {
+ if (CollectionUtils.isEmpty(collection)) {
+ return new HashMap<>(0);
+ }
+
+ return collection.stream().collect(Collectors.toMap(keyFunction, valueFunction, mergeFunction));
+ }
+
+ public static BinaryOperator pickFirst() {
+ return (k1, k2) -> k1;
+ }
+
+ public static BinaryOperator pickSecond() {
+ return (k1, k2) -> k2;
+ }
+
+}
diff --git a/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/collectiontomap/TestCollectionToMap.java b/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/collectiontomap/TestCollectionToMap.java
new file mode 100644
index 0000000..9cb993e
--- /dev/null
+++ b/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/collectiontomap/TestCollectionToMap.java
@@ -0,0 +1,33 @@
+package org.example.collectiontomap;
+
+import com.google.common.collect.Lists;
+import org.example.entity.OrderItem;
+import org.junit.Test;
+
+import java.util.List;
+import java.util.Map;
+
+// 对Java Stream进行二次封装写的10个方法 精通lambda表达式
+public class TestCollectionToMap {
+
+ @Test
+ public void testToMap() {
+ List orderItems = Lists.newArrayList(
+ new OrderItem(1, 5d, "手表"),
+ new OrderItem(2, 6d, "机器人"),
+ new OrderItem(3, 8d, "手机")
+ );
+ Map map = CollectionToMap.toMap(orderItems, OrderItem::getOrderId);
+ }
+
+ @Test
+ public void testToMapV2() {
+ List orderItems = Lists.newArrayList(
+ new OrderItem(1, 5d, "手表"),
+ new OrderItem(2, 6d, "机器人"),
+ new OrderItem(3, 8d, "手机")
+ );
+ Map map = CollectionToMap.toMap(orderItems, OrderItem::getOrderId, OrderItem::getPrice);
+ }
+
+}
diff --git a/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/entity/OrderItem.java b/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/entity/OrderItem.java
new file mode 100644
index 0000000..3470d65
--- /dev/null
+++ b/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/entity/OrderItem.java
@@ -0,0 +1,12 @@
+package org.example.entity;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+@Data
+@AllArgsConstructor
+public class OrderItem {
+ private int orderId;
+ private double price;
+ private String name;
+}
diff --git a/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/listtoset/ListToSet.java b/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/listtoset/ListToSet.java
new file mode 100644
index 0000000..afbe77b
--- /dev/null
+++ b/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/listtoset/ListToSet.java
@@ -0,0 +1,20 @@
+package org.example.listtoset;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+public class ListToSet {
+
+
+ public static List mapToList(Collection collection, Function mapper) {
+ return collection.stream().map(mapper).collect(Collectors.toList());
+ }
+
+ public static Set mapToSet(Collection collection, Function mapper) {
+ return collection.stream().map(mapper).collect(Collectors.toSet());
+ }
+
+}
diff --git a/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/listtoset/TestListToSet.java b/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/listtoset/TestListToSet.java
new file mode 100644
index 0000000..bd42e3c
--- /dev/null
+++ b/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/listtoset/TestListToSet.java
@@ -0,0 +1,31 @@
+package org.example.listtoset;
+
+import com.google.common.collect.Lists;
+import org.example.entity.OrderItem;
+import org.junit.Test;
+
+import java.util.List;
+import java.util.Set;
+//List、Set 类型之间的转换
+public class TestListToSet {
+ @Test
+ public void testMapToList2() {
+ List orderItems = Lists.newArrayList(
+ new OrderItem(1, 5d, "手表"),
+ new OrderItem(2, 6d, "机器人"),
+ new OrderItem(3, 8d, "手机")
+ );
+ List orderIds = ListToSet.mapToList(orderItems, (item) -> item.getOrderId());
+ }
+
+ @Test
+ public void testMapToSetV2() {
+ List orderItems = Lists.newArrayList(
+ new OrderItem(1, 5d, "手表"),
+ new OrderItem(2, 6d, "机器人"),
+ new OrderItem(3, 8d, "手机")
+ );
+ Set orderIds = ListToSet.mapToSet(orderItems, (item) -> item.getOrderId());
+ }
+
+}
diff --git a/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/maptovalue/MapToValue.java b/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/maptovalue/MapToValue.java
new file mode 100644
index 0000000..9d9b7ab
--- /dev/null
+++ b/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/maptovalue/MapToValue.java
@@ -0,0 +1,42 @@
+package org.example.maptovalue;
+
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.BiFunction;
+import java.util.function.BinaryOperator;
+import java.util.stream.Collectors;
+
+
+public class MapToValue {
+
+ public static Map convertMapValue(Map map,
+ BiFunction valueFunction,
+ BinaryOperator mergeFunction) {
+ if (isEmpty(map)) {
+ return new HashMap<>();
+ }
+ return map.entrySet().stream().collect(Collectors.toMap(
+ e -> e.getKey(),
+ e -> valueFunction.apply(e.getKey(), e.getValue()),
+ mergeFunction
+ ));
+ }
+
+ private static boolean isEmpty(Map map) {
+ return map == null || map.isEmpty();
+ }
+
+ public static Map convertMapValue(Map originMap, BiFunction valueConverter) {
+ return convertMapValue(originMap, valueConverter, MapToValue.pickSecond());
+ }
+
+ public static BinaryOperator pickFirst() {
+ return (k1, k2) -> k1;
+ }
+
+ public static BinaryOperator pickSecond() {
+ return (k1, k2) -> k2;
+ }
+
+}
diff --git a/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/maptovalue/TestMapToValue.java b/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/maptovalue/TestMapToValue.java
new file mode 100644
index 0000000..337f8f5
--- /dev/null
+++ b/技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/maptovalue/TestMapToValue.java
@@ -0,0 +1,32 @@
+package org.example.maptovalue;
+
+
+import com.google.common.collect.Lists;
+import org.example.collectiontomap.CollectionToMap;
+import org.example.entity.OrderItem;
+import org.junit.Test;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Map格式转换
+ * 转换 Map 的 Value
+ * 将 Map 中的value 转化为 Map
+ * value 转化时,lamada表达式可以使用(v)->{}, 也可以使用 (k,v)->{ }。
+ */
+public class TestMapToValue {
+
+ @Test
+ public void testConvertValue() {
+ List orderItems = Lists.newArrayList(
+ new OrderItem(1, 5d, "手表"),
+ new OrderItem(2, 6d, "机器人"),
+ new OrderItem(3, 8d, "手机")
+ );
+ Map map = CollectionToMap.toMap(orderItems, OrderItem::getOrderId);
+ Map convertMap = MapToValue.convertMapValue(map, (id, item) -> id + item.getName());
+
+ }
+
+}