import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import lombok.Builder;
import lombok.Data;
import org.junit.Test;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* @author wangXiaoMing
* @date 2022/04/27 10:46
*/
public class LambdaTest {
@Data
@Builder
private static class DemoEntity {
private Integer id;
private String name;
}
@Test
public void baseTest() {
List<DemoEntity> list = new ArrayList<>();
// 1.取list中的两个值成map - 注意键不能重复和null
Map<Integer, String> stringMap = list.stream().collect(Collectors.toMap(DemoEntity::getId, DemoEntity::getName));
// 2.取list中的某个值成list或set
List<Integer> integers = list.stream().map(DemoEntity::getId).collect(Collectors.toList());
Set<String> strings = list.stream().map(DemoEntity::getName).collect(Collectors.toSet());
// 3.groupBy操作
list.add(DemoEntity.builder().id(2).name("a").build());
list.add(DemoEntity.builder().id(3).name("a").build());
list.add(DemoEntity.builder().id(1).name("b").build());
// {a=[LambdaTest.DemoEntity(id=2, name=a), LambdaTest.DemoEntity(id=3, name=a)], b=[LambdaTest.DemoEntity(id=1, name=b)]}
Map<String, List<DemoEntity>> stringListMap = list.stream().collect(Collectors.groupingBy(DemoEntity::getName));
// [[LambdaTest.DemoEntity(id=2, name=a), LambdaTest.DemoEntity(id=3, name=a)], [LambdaTest.DemoEntity(id=1, name=b)]]
// 注: CollUtil 为hutool中的工具类
List<List<DemoEntity>> groupByField = CollUtil.groupByField(list, "name");
// 4.对list排序
// 4.1 按照name从小到大排序
// [LambdaTest.DemoEntity(id=2, name=a), LambdaTest.DemoEntity(id=3, name=a), LambdaTest.DemoEntity(id=1, name=b)]
List<DemoEntity> sortedStringList = list.stream().sorted(Comparator.comparing(DemoEntity::getName)).collect(Collectors.toList());
// 4.2 按照id从小到大
// [LambdaTest.DemoEntity(id=1, name=b), LambdaTest.DemoEntity(id=2, name=a), LambdaTest.DemoEntity(id=3, name=a)]
List<DemoEntity> sortedIntegerList = list.stream().sorted(Comparator.comparing(DemoEntity::getId)).collect(Collectors.toList());
// 4.3 自定义顺序(id降序)
// [LambdaTest.DemoEntity(id=3, name=a), LambdaTest.DemoEntity(id=2, name=a), LambdaTest.DemoEntity(id=1, name=b)]
List<DemoEntity> orderSortedIntegerList = list.stream().sorted(Comparator.comparing(DemoEntity::getId).reversed()).collect(Collectors.toList());
List<DemoEntity> orderSortedIntegerList1 = list.stream().sorted((i, j) -> j.getId().compareTo(i.getId())).collect(Collectors.toList());
// 4.4 使用hutool工具类排序
List<DemoEntity> hutoolSortList = ListUtil.sort(list, Comparator.comparing(DemoEntity::getId).reversed());
// 4.5 补充对map排序
Map<String, Integer> map = new HashMap<String, Integer>() {{
put("d", 4);
put("e", 6);
put("f", 5);
}};
// 4.5.1 map转集合排序
//{d=4, f=5, e=6}
final Map<String, Integer> sortedMap = new LinkedHashMap<>();
map.entrySet().stream().sorted(Map.Entry.comparingByValue())
.collect(Collectors.toList()).forEach(u -> sortedMap.put(u.getKey(), u.getValue()));
// 4.5.2 hutool对map排序
// {d=4, f=5, e=6}
LinkedHashMap<String, Integer> sortByEntry = CollUtil.sortByEntry(map, Map.Entry.comparingByValue());
// {e=6, f=5, d=4}
Map<String, Integer> sortByEntry1 = MapUtil.sortByValue(map, true);
// 4.5.3 当按照值排序, 且值中有null值时
map.put("a", null);
// {a=null, d=4, f=5, e=6}
LinkedHashMap<String, Integer> sortByEntry2 = CollUtil.sortByEntry(map, Map.Entry.comparingByValue(Comparator.nullsFirst(Integer::compareTo)));
// 5.取两个list的交集 - 若为对象数组, 在流中使用map即可
// 参考: https://blog.csdn.net/a646705816/article/details/111927462
List<Integer> listA = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> listB = Arrays.asList(1, 4, 5, 6, 2);
// [1, 2, 4, 5]
List<Integer> intersect = listA
.stream()
.filter(a -> listB.stream().anyMatch(b -> Objects.equals(a, b)))
.collect(Collectors.toList());
// 6.取差集 换成noneMatch即可
// [3]
List<Integer> intersect1 = listA
.stream()
.filter(a -> listB.stream().noneMatch(b -> Objects.equals(a, b)))
.collect(Collectors.toList());
// 7.求和
// 15
long sum = listA.stream().collect(Collectors.summarizingInt(u -> u)).getSum();
// 6
long sum1 = list.stream().collect(Collectors.summarizingInt(DemoEntity::getId)).getSum();
// 6
int sum2 = list.stream().mapToInt(DemoEntity::getId).sum();
// 8.取两个List中的值组成map, 要求list大小相等
// 使用hutool工具类深拷贝
List<DemoEntity> list1 = BeanUtil.copyToList(list, DemoEntity.class);
// 对于对象的性能未作测试 !!! 可能时间会长
// {1=b, 2=a, 3=a}
Map<Integer, String> collect = IntStream.range(0, list.size()).boxed().collect(Collectors.toMap(list.stream().map(DemoEntity::getId).collect(Collectors.toList())::get,
list1.stream().map(DemoEntity::getName).collect(Collectors.toList())::get));
// 9.其他用法如: filter(过滤掉不想要的数据), forEach, remove, removeIf, merge, distinct等未做整理
// 参考:
// https://stackoverflow.com/questions/46873916/java-8-merge-2-string-lists-into-map
// https://blog.csdn.net/a646705816/article/details/111927462
// https://objcoding.com/2019/03/04/lambda/
// https://blog.51cto.com/u_14479502/3115693
// https://blog.csdn.net/u014231523/article/details/102535902
// https://copyfuture.com/blogs-details/202202120151292216 - 其中对排序有第二排序位的样例
}
}
扩展
// 用id1分组(结果是键id, 值是对象list), 取值(对象list)中的id2成List<Integer>
Map<Integer, List<Integer>> id1Id2ListMap = list
.stream()
.collect(Collectors.groupingBy(Entity::getId1, Collectors.mapping(Entity::getId2, Collectors.toList())));