public class streamTest {
/**前端 js
* let arr = [{id:1,name:"ls"},{id:2,name:"zs"}]
* let res = arr.map(item => {
* let name = item.id;
* var tem = new Map()
* tem.set(item.id, item);
* return tem;
* })
*
* [
* Map { 1 => { id: 1, name: 'ls' } },
* Map { 2 => { id: 2, name: 'zs' } }
* ]
* */
/**
* 关于 Collectors.toMap 方法
* Collectors.toMap 有三个重载方法:
*
* toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper);
* toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper,
* BinaryOperator<U> mergeFunction);
* toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper,
* BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier);
* 参数含义分别是:
*
* keyMapper:Key 的映射函数
*
* valueMapper:Value 的映射函数
*
* mergeFunction:当 Key 冲突时,调用的合并方法
*
* mapSupplier:Map 构造器,在需要返回特定的 Map 时使用
* */
public static void main(String[] args) {
ArrayList<UserInfo> arr = CollUtil.newArrayList();
UserInfo userInfo = new UserInfo("lisi", "111");
UserInfo userInfo2 = new UserInfo("zhangsan", "222");
UserInfo userInfo3 = new UserInfo("zhangsan", "333");
UserInfo userInfo4 = new UserInfo("beifeng", "444");
arr.add(userInfo);
arr.add(userInfo2);
arr.add(userInfo3);
arr.add(userInfo4);
System.out.println(arr);
// HashMap<String, String> a = CollUtil.newHashMap();
// a.put("A","aaa");
// a.put("B", "bbb");
// a.put("A", "ccc");
// System.out.println(a);
//将一个数组转换成 一个map对象;key(string) -> value(string)
// Map<String, String> map = arr.stream().collect(Collectors.toMap(UserInfo::getUsername, UserInfo::getPassword));
// System.out.println(map);
//{lisi=111, zhangsan=222}
//将一个数组转换成 一个map对象;key(string) -> value(UserInfo)
// Map<String, UserInfo> map2 = arr.stream().collect(Collectors.toMap(UserInfo::getUsername, v -> v));
// System.out.println(map2);
//Exception in thread "main" java.lang.IllegalStateException: Duplicate key 222
// at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
// at java.util.HashMap.merge(HashMap.java:1253)
// at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
// at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
// at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
// at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
// at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
// at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
// at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
// at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
// at com.ruoyi.common.demo.stream.streamTest.main(streamTest.java:46)
// 如果有重复的key,第三个参数处理合并
Map<String, UserInfo> map3 = arr.stream().collect(Collectors.toMap(UserInfo::getUsername, v -> v, (v1, v2) -> v1));
System.out.println(map3);
//{lisi=UserInfo(username=lisi, password=111), zhangsan=UserInfo(username=zhangsan, password=222)}
// 如果有重复的key,第三个参数处理合并, 如果处理map对象的顺序
Map<String, UserInfo> map4 = arr.stream().collect(Collectors.toMap(UserInfo::getUsername, v -> v, (v1, v2) -> v1, TreeMap:: new));
System.out.println(map4);
//{beifeng=UserInfo(username=beifeng, password=444), lisi=UserInfo(username=lisi, password=111), zhangsan=UserInfo(username=zhangsan, password=222)}
//将传过来的数据 List 映射成功 另一个对象的信息
List<UserInfo> t = arr.stream().filter(item -> Objects.nonNull(arr)).map(item -> new UserInfo("t", "1")).collect(Collectors.toList());
System.out.println(t);
//eg
receiveThirdReqDtOList.stream()
.filter(receiveThirdReqDtO -> StrUtil.isNotBlank(receiveThirdReqDtO.getCouponAtno()))
.map(receiveThirdReqDtO -> shopPoolDao.getCouponDetail(null, receiveThirdReqDtO.getCouponAtno()))
.collect(Collectors.toList());
}
}
常用使用技巧
//给你下面一个字符串数组,如果用stream api来实现,找出以字符'a'开头长度最大的字符串,使用stream api该怎么实现呢?
List<String> list = Arrays.asList("abb", "abcd", "fegc", "efe", "adfes");
System.out.println(list);
//过滤首字母是 a
List<String> a = list.stream().filter(item -> item.startsWith("a")).collect(Collectors.toList());
System.out.println(a);
//
int a2 = list.stream().filter(item -> item.startsWith("a")).mapToInt(item -> item.length()).max().orElse(0);
System.out.println(a2);
比如过滤自己相关的 id集合
//先过滤再映射自己需要的字段
List<String> collect = arr.stream().filter(v -> v.getPassword().indexOf("2") < 0)
.map(UserInfo::getUsername)
.collect(Collectors.toList());
System.out.println(collect);//[lisi, zhangsan, beifeng]