Browse Source

技术总监写的十个方法,让我精通了lambda表达式

pull/2/head
程序员蜗牛 1 year ago
parent
commit
200deeeecb
  1. 57
      技术总监写的十个方法,让我精通了lambda表达式/lambdatest/pom.xml
  2. 29
      技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/collectiontolist/CollectionToListOrSet.java
  3. 35
      技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/collectiontolist/TestCollection.java
  4. 39
      技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/collectiontomap/CollectionToMap.java
  5. 33
      技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/collectiontomap/TestCollectionToMap.java
  6. 12
      技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/entity/OrderItem.java
  7. 20
      技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/listtoset/ListToSet.java
  8. 31
      技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/listtoset/TestListToSet.java
  9. 42
      技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/maptovalue/MapToValue.java
  10. 32
      技术总监写的十个方法,让我精通了lambda表达式/lambdatest/src/main/java/org/example/maptovalue/TestMapToValue.java

57
技术总监写的十个方法,让我精通了lambda表达式/lambdatest/pom.xml

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>lambdatest</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>32.1.3-jre</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>

29
技术总监写的十个方法,让我精通了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 <T> List<T> toList(Collection<T> collection) {
if (collection == null) {
return new ArrayList<>();
}
if (collection instanceof List) {
return (List<T>) collection;
}
return collection.stream().collect(Collectors.toList());
}
public static <T> Set<T> toSet(Collection<T> collection) {
if (collection == null) {
return new HashSet<>();
}
if (collection instanceof Set) {
return (Set<T>) collection;
}
return collection.stream().collect(Collectors.toSet());
}
}

35
技术总监写的十个方法,让我精通了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<OrderItem> orderItems = Lists.newArrayList(
new OrderItem(1, 5d, "手表"),
new OrderItem(2, 6d, "机器人"),
new OrderItem(3, 8d, "手机")
);
List<OrderItem> list = CollectionToListOrSet.toList(orderItems);
}
//将集合 Collection 转化为 Set
@Test
public void testToSet() {
List<OrderItem> orderItems = Lists.newArrayList(
new OrderItem(1, 5d, "手表"),
new OrderItem(2, 6d, "机器人"),
new OrderItem(3, 8d, "手机")
);
Set<OrderItem> set = CollectionToListOrSet.toSet(orderItems);
}
}

39
技术总监写的十个方法,让我精通了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 <T, K> Map<K, T> toMap(Collection<T> collection, Function<? super T, ? extends K> keyMapper) {
return toMap(collection, keyMapper, Function.identity());
}
public static <T, K, V> Map<K, V> toMap(Collection<T> collection, Function<? super T, ? extends K> keyFunction, Function<? super T, ? extends V> valueFunction) {
return toMap(collection, keyFunction, valueFunction, pickSecond());
}
public static <T, K, V> Map<K, V> toMap(Collection<T> collection, Function<? super T, ? extends K> keyFunction, Function<? super T, ? extends V> valueFunction, BinaryOperator<V> mergeFunction) {
if (CollectionUtils.isEmpty(collection)) {
return new HashMap<>(0);
}
return collection.stream().collect(Collectors.toMap(keyFunction, valueFunction, mergeFunction));
}
public static <T> BinaryOperator<T> pickFirst() {
return (k1, k2) -> k1;
}
public static <T> BinaryOperator<T> pickSecond() {
return (k1, k2) -> k2;
}
}

33
技术总监写的十个方法,让我精通了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<OrderItem> orderItems = Lists.newArrayList(
new OrderItem(1, 5d, "手表"),
new OrderItem(2, 6d, "机器人"),
new OrderItem(3, 8d, "手机")
);
Map<Integer, OrderItem> map = CollectionToMap.toMap(orderItems, OrderItem::getOrderId);
}
@Test
public void testToMapV2() {
List<OrderItem> orderItems = Lists.newArrayList(
new OrderItem(1, 5d, "手表"),
new OrderItem(2, 6d, "机器人"),
new OrderItem(3, 8d, "手机")
);
Map<Integer, Double> map = CollectionToMap.toMap(orderItems, OrderItem::getOrderId, OrderItem::getPrice);
}
}

12
技术总监写的十个方法,让我精通了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;
}

20
技术总监写的十个方法,让我精通了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 <T, R> List<R> mapToList(Collection<T> collection, Function<T, R> mapper) {
return collection.stream().map(mapper).collect(Collectors.toList());
}
public static <T, R> Set<R> mapToSet(Collection<T> collection, Function<T, R> mapper) {
return collection.stream().map(mapper).collect(Collectors.toSet());
}
}

31
技术总监写的十个方法,让我精通了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<OrderItem> orderItems = Lists.newArrayList(
new OrderItem(1, 5d, "手表"),
new OrderItem(2, 6d, "机器人"),
new OrderItem(3, 8d, "手机")
);
List<Integer> orderIds = ListToSet.mapToList(orderItems, (item) -> item.getOrderId());
}
@Test
public void testMapToSetV2() {
List<OrderItem> orderItems = Lists.newArrayList(
new OrderItem(1, 5d, "手表"),
new OrderItem(2, 6d, "机器人"),
new OrderItem(3, 8d, "手机")
);
Set<Integer> orderIds = ListToSet.mapToSet(orderItems, (item) -> item.getOrderId());
}
}

42
技术总监写的十个方法,让我精通了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 <K, V, C> Map<K, C> convertMapValue(Map<K, V> map,
BiFunction<K, V, C> valueFunction,
BinaryOperator<C> 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 <K, V> boolean isEmpty(Map<K,V> map) {
return map == null || map.isEmpty();
}
public static <K, V, C> Map<K, C> convertMapValue(Map<K, V> originMap, BiFunction<K, V, C> valueConverter) {
return convertMapValue(originMap, valueConverter, MapToValue.pickSecond());
}
public static <T> BinaryOperator<T> pickFirst() {
return (k1, k2) -> k1;
}
public static <T> BinaryOperator<T> pickSecond() {
return (k1, k2) -> k2;
}
}

32
技术总监写的十个方法,让我精通了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<Long, OrderItem> 中的value 转化为 Map<Long, Double>
* value 转化时lamada表达式可以使用v)->{} 也可以使用 kv->{ }
*/
public class TestMapToValue {
@Test
public void testConvertValue() {
List<OrderItem> orderItems = Lists.newArrayList(
new OrderItem(1, 5d, "手表"),
new OrderItem(2, 6d, "机器人"),
new OrderItem(3, 8d, "手机")
);
Map<Integer, OrderItem> map = CollectionToMap.toMap(orderItems, OrderItem::getOrderId);
Map<Integer, String> convertMap = MapToValue.convertMapValue(map, (id, item) -> id + item.getName());
}
}
Loading…
Cancel
Save