package com.learn.stream.lambda;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Sets;
import com.learn.bean.CouponInfo;
import com.learn.utils.LambdaUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.Before;
import org.junit.Test;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author kermit.liu on 2018/10/11
*/
@Slf4j
public class LambdaTest {
private List couponInfoList;
private List strList;
private List intList;
@Before
public void init() {
CouponInfo couponInfo1 = new CouponInfo(123L, 10001, "5元现金券", 1);
CouponInfo couponInfo2 = new CouponInfo(124L, 10001, "10元现金券",1);
CouponInfo couponInfo3 = new CouponInfo(125L, 10002, "全场9折",2);
CouponInfo couponInfo4 = new CouponInfo(126L, 10002, "全场8折", 2);
CouponInfo couponInfo5 = new CouponInfo(127L, 10003, "全场7折", 2);
couponInfoList = new ArrayList<>();
couponInfoList.add(couponInfo1);
couponInfoList.add(couponInfo2);
couponInfoList.add(couponInfo3);
couponInfoList.add(couponInfo4);
couponInfoList.add(couponInfo5);
couponInfoList = new ArrayList<>();
couponInfoList.add(couponInfo1);
couponInfoList.add(couponInfo2);
couponInfoList.add(couponInfo3);
couponInfoList.add(couponInfo4);
couponInfoList.add(couponInfo5);
strList = Arrays.asList(new String[]{"A", "S", "D", "F", "X", "C", "Y", "H", "", null});
intList = Arrays.asList(new Integer[]{1, 2, 3, 4, 5, 6, 6, 2, 3});
}
/**
* 迭代 forEach
*/
@Test
public void testForEach() {
strList.stream().forEach(System.out::println);
strList.stream().forEach(e -> System.out.print(e));
System.out.println();
strList.forEach(System.out::print);
}
/**
* 过滤 filter
*/
@Test
public void testFilter() {
List list = strList.stream().filter(x -> StringUtils.isNotBlank(x)).collect(Collectors.toList());
System.out.println(list);
List list1 = strList.stream().filter(StringUtils::isNotBlank).collect(Collectors.toList());
System.out.println(list1);
List list2 = intList.stream().distinct().collect(Collectors.toList());
System.out.println(list2);
List list3 = couponInfoList.stream().filter(x -> x.getMerchantId() != 10001).collect(Collectors.toList());
System.out.println(list3);
List collect = couponInfoList.stream().filter(LambdaUtil.distinctByKey(CouponInfo::getCouponType)).collect(Collectors.toList());
System.out.println(collect);
List collect1 = couponInfoList.stream().filter(LambdaUtil.distinctByKey(CouponInfo::getCouponType)).map(CouponInfo::getCouponType).collect(Collectors.toList());
System.out.println(collect1);
List CouponList = couponInfoList.stream().map(CouponInfo::getCouponType).collect(Collectors.toList());
System.out.println(CouponList);
HashSet set = Sets.newHashSet(CouponList);
System.out.println(set);
}
/**
* limit
*/
@Test
public void testLimit() {
List list = strList.stream().limit(3).collect(Collectors.toList());
System.out.println(list);
}
/**
* 排序 sorted
*/
@Test
public void testSorted() {
List list = intList.stream().sorted().collect(Collectors.toList());
System.out.println(list);
// 倒序
List list2 = intList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
System.out.println(list2);
List list3 = strList.stream().sorted(Comparator.nullsLast(Comparator.naturalOrder())).collect(Collectors.toList());
List list4 = strList.stream().sorted(Comparator.nullsLast(Comparator.reverseOrder())).collect(Collectors.toList());
System.out.println(list3);
System.out.println(list4);
List list5 = couponInfoList.stream().sorted(Comparator.comparing(CouponInfo::getId)).collect(Collectors.toList());
List list6 = couponInfoList.stream().sorted(Comparator.comparing(CouponInfo::getId).reversed()).collect(Collectors.toList());
List list51 = list5.stream().map(e -> e.getId()).collect(Collectors.toList());
List list61 = list6.stream().map(e -> e.getId()).collect(Collectors.toList());
System.out.println(list51);
System.out.println(list61);
}
/**
* 拼接字符串 joining
*/
@Test
public void testJoining() {
String collectStr = couponInfoList.stream().map(CouponInfo::getCouponName).collect(Collectors.joining(",", "[", "]"));
System.out.println(collectStr.getClass().getName() + ":" + collectStr);
List collectList = couponInfoList.stream().map(CouponInfo::getCouponName).collect(Collectors.toList());
System.out.println(collectList.getClass().getName() + ":" + collectList);
System.out.println("StringUtils.join() : " + StringUtils.join(collectList, ","));
}
/**
* map
* 对每个元素进行处理,相当于MapReduce中的map阶段
* Collectors.mapping()类似
*/
@Test
public void testMap() {
List list = intList.stream().map(e -> 2 * e).collect(Collectors.toList());
System.out.println(list);
}
/**
* 转成Map
*
* 特别注意,key不能重复,如果重复的话默认会报错,可以指定key重复的时候怎么处理
*
* 例如:Map studentIdToStudent = students.stream().collect(toMap(Student::getId, Functions.identity());
*/
@Test
public void testToMap() {
// 因为ID不重复,所以这里这么写没问题;但如果key换成CouponInfo::getMerchantId就有问题了
// java.lang.IllegalStateException: Duplicate key
Map map = couponInfoList.stream().collect(Collectors.toMap(CouponInfo::getId, Function.identity()));
// 这里重复的处理方式就是用后者覆盖前者
Map map2 = couponInfoList.stream().collect(Collectors.toMap(CouponInfo::getMerchantId, Function.identity(), (c1, c2) -> c2));
Map map3 = couponInfoList.stream().collect(Collectors.toMap(CouponInfo::getMerchantId, Function.identity(), (c1, c2) -> (c1.getId() > c2.getId() ? c2 : c1)));
System.out.println(map);
System.out.println(map2);
System.out.println(map3);
}
/**
* 数据分块 partitioningBy 分解成两个集合
*/
@Test
public void testPartitioningBy() {
Map> collectListMap = couponInfoList.stream().filter(couponInfo -> null != couponInfo.getCouponType()).collect(Collectors.partitioningBy(couponInfo -> couponInfo.getCouponType() == 1));
System.out.println(collectListMap.get(true));
System.out.println(collectListMap.get(false));
}
/**
* 数据分组 groupingBy
*/
@Test
public void testGroupBy() {
Map> map = couponInfoList.stream().collect(Collectors.groupingBy(CouponInfo::getMerchantId));
// 组合收集器
Map map2 = couponInfoList.stream().collect(Collectors.groupingBy(CouponInfo::getMerchantId, Collectors.counting()));
// 组合收集器
Map> map3 = couponInfoList.stream().collect(Collectors.groupingBy(CouponInfo::getMerchantId, Collectors.mapping(CouponInfo::getCouponName, Collectors.toSet())));
System.out.println(map);
System.out.println(map2);
System.out.println(map3);
}
/**
* 数值统计 Max Min Average Sum
*/
@Test
public void testSum() {
IntSummaryStatistics summaryStatistics = intList.stream().mapToInt(x -> x).summaryStatistics();
System.out.println(summaryStatistics.getMax());
System.out.println(summaryStatistics.getMin());
System.out.println(summaryStatistics.getAverage());
System.out.println(summaryStatistics.getSum());
}
/**
* 给某个字段赋值
*/
@Test
public void testPeek() {
List peelCouponInfoList = couponInfoList.stream().peek(couponInfo -> couponInfo.setCouponName("peekCoupon")).collect(Collectors.toList());
log.info(JSON.toJSONString(peelCouponInfoList));
}
}
工具类【LambdaUtil】
package com.learn.utils;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* @author kermit.liu on 2018/9/21
*/
public class LambdaUtil {
/**
* 配合lambda中的filter 根据对象中的某个属性去重
*
* @param keyExtractor 类名::getXXX
* @param 泛型
* @return 对象
*/
public static Predicate distinctByKey(Function super T, ?> keyExtractor) {
Map