当前位置: 首页 > 后端技术 > Java

[Java]流的用法

时间:2023-04-01 17:13:13 Java

将列表转换为mapMapitemMap=items.stream().collect(Collectors.toMap(ItemTO::getEventId,Function.identity(),(v1,v2)->v1));List中的对象取出某个属性值作为一个list//去重Listids=items.stream().map(ItemTO::getId).distinct().collect(Collectors.toList());//不Re-Listids=items.stream().map(ItemTO::getId).collect(Collectors.toList());将列表转换为MapMap>itemListMap=itemList.stream().collect(Collectors.groupingBy(item->item.getId()));MapitemMap=items.stream().collect(Collectors.toMap(ItemTO::getEventId,Function.identity(),(v1,v2)->v1));将列表中的字符串转换为longListidList=groupList.stream().map(group->Long.parseLong(group.getGroupLeaderId())).collect(Collectors.toList());过滤操作(t->StoreItemTypeEnum.PLATFORM.getType().equals(t.getType())).map(StoreItemIndexResultTO::getIid).collect(Collectors.toList());//过滤购买数量为的skuListskuCollectList=skuInfoTOList.stream().filter(e->e.getNum()>0).collect(Collectors.toList());/***过滤顺序列表中的指定oid*@parameffectiveLists*@paramoid*@return*/privateListfilterOrder(ListitmeList,longoid){returnitmeList.stream().filter(p->{if(p.getOid().equals(oid)){returnfalse;}returntrue;}).collect(Collectors.toList());}使用list中对象的两个属性值分别作为map的key和valueitemList.stream()。filter(t->t.getId()!=null).collect(Collectors.toMap(ItemTO::getId,ItemTO::getName,(k1,k2)->k2));操作列表//intersectionListintersection=list1.stream().filter(item->list2.contains(item)).collect(toList());//Difference(list1-list2)Listreduce1=list1.stream().filter(item->!list2.contains(item)).collect(toList());//差异(list2-list1)Listreduce2=list2.stream().filter(item->!list1.contains(item)).collect(toList());//unionListlistAll=list1.parallelStream().collect(toList());ListlistAll2=list2.parallelStream().collect(toList());listAll.addAll(listAll2);//去重unionListlistAllDistinct=listAll.stream().distinct().collect(toList());System.out.println("---获取去重unionlistAllDistinct---");//循环输出listAllDistinct.parallelStream().forEachOrdered(System.out::println);map操作,remove//通过value().removeIf(value->!value.contains("1"));//通过key移除map.keySet().removeIf(key->key!=1);//通过键或值删除映射。entrySet().removeIf(入口->entry.getKey()!=1);排序操作//升序itemList=itemList.stream().sorted(Comparator.comparingInt(ItemTO::getPtPrice)).collect(Collectors.toList());//降序itemList=itemList.stream().sorted(Comparator.comparingInt(ItemTO::getPtPrice).reversed()).collect(Collectors.toList());groupBy操作//aMap>exhibitionPitemMap=list.stream().collect(Collectors.groupingBy(TestDTO1::getLevle1CategoryId,Collectors.mapping(TestDTO1::getPitemId,Collectors.toList())));//bMap>categoryPitemMap=list.stream().collect(Collectors.groupingBy(TestDTO2::getLevle1CategoryId));map转list使用方法Mapmap=newHashMap<>();//将所有Map键转换为ListListresult=newArrayList(map.keySet());//将所有Map值转换为ListListresult2=newArrayList(map.values());//Java8,将所有Map键转换为ListListresult3=map.keySet().stream().collect(Collectors.toList());//Java8,将所有Map值转换为ListListresult4=map.values().stream().collect(Collectors.toList());//Java8,看起来有点长,但是你可以享受流功能,如过滤器等修改list中的元素值list.stream().filter(a->过滤条件).forEach(b->b.setOrg("1"));使用list中元素的两个属性分别作为map的key和valueMapidNameMap=userList.stream().collect(Collectors.toMap(UserDO::getId,UserDO::getName,(v1,v2)->v1));将映射中的键从String转换为LongMapmap1=map.entrySet().stream().collect(Collectors.toMap(e->Long.parseLong(e.getKey()),Map.Entry::获取值));

最新推荐
猜你喜欢