首页 > 其他分享 >Page.map方法的使用

Page.map方法的使用

时间:2022-10-25 09:00:07浏览次数:47  
标签:Function map converter Page collect 方法 page

Page.map方法的使用

1、前言

日常工作中,我们常常会有这样的场景:分页查询得到了结果,需要对dto的某个单独字段将进行赋值,这时候我们就会用到Page分页对象提供的map方法,用来转换Page内部对象。

2、实例

@Override
public ResponsePageVO<ProductStatDto> queryAll(ProductStatQueryCriteria criteria, Pageable pageable) {
    // 分页查询得到结果
    Page<ProductStat> page = this.queryPage(criteria, pageable);
    // 得到dto集合
    List<ProductStatDto> contentList = productStatMapper.toDto(page.getContent());
    // 分别进行set值,并得到一个map
    Map<Long, ProductStatDto> collect = contentList.stream().peek(x -> x.setCraftDto(craftService.findById(x.getCraftId())))
            .collect(Collectors.toMap(ProductStatDto::getProductRowId, x -> x));
    //page.map转换内部对象
    Page<ProductStatDto> pageMap = page.map(x -> collect.get(x.getProductRowId()));
    return PageUtil.toPageVo(pageMap);
}

3、源码

<U> Page<U> map(Function<? super T, ? extends U> converter);

page.map需要我们传入一个converter

@Override
public <U> Page<U> map(Function<? super T, ? extends U> converter) {
   return new PageImpl<>(getConvertedContent(converter), getPageable(), total);
}

主要是实现了getConvertedContent(converter)方法

protected <U> List<U> getConvertedContent(Function<? super T, ? extends U> converter) {
   Assert.notNull(converter, "Function must not be null!");
   return this.stream().map(converter::apply).collect(Collectors.toList());
}

stream().map每一个操作,最后收集list。

标签:Function,map,converter,Page,collect,方法,page
From: https://www.cnblogs.com/jspider/p/16823758.html

相关文章

  • Kubernetes-1.25.2 Relevant Download Page
    一、Kubernetes-1.25.2RelevantDownloadPage1 kubernetes页面链接:https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG/CHANGELOG-1.25.md#server-bin......
  • 银河麒麟安装nmon以及rpc.rstatd的方法
    银河麒麟安装nmon以及rpc.rstatd的方法 背景说明随着公司业务的发展,需要在ARM环境上面进行性能测试.为了进行ARM环境的验证,需要一些组件进行资料收集.比较好的......
  • 全球名校AI课程库(15)| Stanford斯坦福 · 线性代数与矩阵方法导论课程『Introduction t
    ......
  • 【JS设计模式笔记】给我一张名片-工厂方法模式(创建型)
    广告展现例如,关于计算机培训广告资源需要投放,一批是Java的用绿色字体,一批是PHP的,用黄色字体,红色背景。//创建Java学科类varJava=function(content){ //将内容保......
  • 数组方法的一些总结
    数组方法总结Push方法:添加一个新元素到数组尾部,并返回修改后数组的长度;letarr1=[1,2,3,4,5,6,7,8,9]this.arr1.push("A")console.log(this.arr1)//1,2,3,4,5,6,7,8,......
  • Semaphore
    1.介绍信号量为多线程协作提供了更为强大的控制方法。广义上说信号量是对锁的扩展,可以指定多个线程同时访问某一资源。2.构造方法publicSemaphore(intpermits){......
  • 6.HashMap源码解析
    1.数据结构如上图所示,HashMap底层的数据结构主要是数组+链表+红黑树。其中当链表的长度大于等于8时,链表会转化成红黑树,当红黑树的大小小于等于6时,红黑树会转化成链表。......
  • 7.TreeMap源码解析
    1.数据结构TreeMap的底层数据结构是红黑树,和HashMap的红黑树结构一样。不同的是,TreeMap利用红黑树左节点小,右节点大的性质,根据key进行排序,使每个元素能够插入到红黑树的适......
  • Python 中3.6及之前版本datetime没有fromisoformat()的解决方法
    Python中可以使用datetime的fromisoformat()方法将字符串转成datetime对象,但只有Python3.7以上才支持,Python3.6及之前的版本不支持,调用会报错:AttributeError:typeobje......
  • currenthashmap(currenthashmap和hashmap的区别)
    concurrenthashmap的读是否要加锁,为什么有并发访问的时候用ConcurrentHashMap,效率比用锁的HashMap好功能上可以,但是毕竟ConcurrentHashMap这种数据结构要复杂些,如果能保证只......