List集合中对象转map

public class QuestionDto {/*** choice_question pid*/@PrimaryKeyprivate String id;/*** 顺序*/@Min(0)private Integer sort;...}

所涉及的代码

Map<String, Integer> collect = list.stream().filter(e -> e.getSort() != null).collect(Collectors.toMap(QuestionDto::getId, QuestionDto::getSort));

所涉及的service层全部代码

@Transactional(rollbackFor = Exception.class)public void updateQuestionSort(String chapterId, List<QuestionDto> list) {List<String> questionPidList = list.stream().map(QuestionDto::getId).collect(Collectors.toList());//获取课程章节相关的习题List<CourseStepQuestion> exercisesOfChapterList =courseChapterQuestionRepository.findAllByChapterPidAndQuestionPidIn(chapterId, questionPidList);if (Integer.compare(list.size(), exercisesOfChapterList.size()) != 0) {throw new BusinessException("course.chapter.exercises.NotMatching.message");}Map<String, Integer> collect = list.stream().filter(e -> e.getSort() != null).collect(Collectors.toMap(QuestionDto::getId, QuestionDto::getSort));exercisesOfChapterList.forEach(e -> {e.setQuestionSort(collect.get(e.getQuestionPid()));});courseChapterQuestionRepository.saveAll(exercisesOfChapterList);}

第二种也是涉及集合转换

    @Transactional(readOnly = true)public List<ChoiceQstDto> findExercisesOfChapter(String chapterId, String relation) {List<CourseStepQuestion> exercisesOfChapterList= courseChapterQuestionRepository.findByChapterPidOrderByCreateTimeDesc(chapterId);List<String> exercisesIdOfChapterList= exercisesOfChapterList.stream().map(CourseStepQuestion::getQuestionPid).collect(Collectors.toList());if (relation.equals(ASSOCIATION)) {// 获取本节课程关联的练习题Map<String, Integer> collect = exercisesOfChapterList.stream().collect(Collectors.toMap(CourseStepQuestion::getQuestionPid, CourseStepQuestion::getQuestionSort));List<ChoiceQuestion> choiceQuestionList = choiceQuestionRepository.findAllById(exercisesIdOfChapterList);return choiceQuestionList.stream().map(e -> new ChapterChoiceQstDto(e.getPid(), e.getTitle(), e.getQuestionType(),e.getContent(), e.getAnswer(), e.getOptions(), collect.get(e.getPid()))).collect(Collectors.toList());}// 默认获取本节课程不关联的练习题List<ChoiceQuestion> choiceQuestionList = choiceQuestionRepository.findByPidNotIn(exercisesIdOfChapterList);return choiceQuestionList.stream().map(e -> new ChapterChoiceQstDto(e.getPid(), e.getTitle(), e.getQuestionType(), e.getContent(), e.getAnswer(), e.getOptions())).collect(Collectors.toList());}
CourseChapterQuestionRepository:
List<CourseStepQuestion> findByChapterPidOrderByCreateTimeDesc(String chapterId);

集合转换Stream流式操作相关推荐

  1. JDK8新特性(三):集合之 Stream 流式操作

    1.Stream流由来 首先我们应该知道:Stream流的出现,主要是用在集合的操作上.在我们日常的工作中,经常需要对集合中的元素进行相关操作.诸如:增加.删除.获取元素.遍历. 最典型的就是集合遍历 ...

  2. Java8 Stream流式操作接口详解

    stream是用于集合使用的流式操作,可使用collection.stream获取流 default Stream<E> stream() {return StreamSupport.st ...

  3. Java8 的 Stream 流式操作之王者归来

    相对于Java8之前的Java的相关操作简直是天差地别,Java8 的流式操作的出现,也很大程度上改变了开发者对于Java的繁琐的操作的印象,从此,Java也走向了函数式编程的道路! 1 流的创建 1 ...

  4. jdk8新特性(二)Stream流式操作

    1.流处理过程 2.Stream流式分类 Stream,IntStream,LongStream,DoubleStream 3.操作符 Stream 的一系列操作必须要使用终止操作,否者整个数据流是不 ...

  5. java8中的Stream流式操作总结,List转Map或List转LinkedHashMap使用Collectors.groupingBy用法

    前言背景描述: 开发使用本来是直接使用数据库的依据SQL进行group By获取到数据表的分组的一个字段的字符串,可是后来字符串越来越多越长,导致的最后的后面长度超多1024个汉字就会被截取,所以需要 ...

  6. 函数式接口和Stream流式思想

    函数式接口和Stream 1. 函数式接口 1.1 函数式接口概述 利用接口操作语法格式,对于方法的声明作出二次封装!!!方法声明:权限修饰符 是否静态 返回值类型 方法名(形式参数列表);对于接口而 ...

  7. Java8种Stream流相关操作——集合的筛选、归约、分组、聚合

    过滤.筛选   filter skip /*** 过滤 筛选*/@Testpublic void test2(){List<String> list = Arrays.asList(&qu ...

  8. list转map stream_advancedday10可变参数不可变集合及Stream流

    可变参数 可变参数指的是,方法参数的个数可以发生改变.但是其本质是一个数组,在方法把可变参数当做数组来使用就可以了. //可变参数的格式:数据类型...参数名public static void 方法 ...

  9. Java8新特性之Stream流式编程

    特地感谢鲁班大叔的分享,原学习地址:Java8 Stream流式编程爱 撸码就是快,流式编程好 代码传家宝 以下是学习过程整理的笔记 1.简介 Stream 流处理,首先要澄清的是 java8 中的 ...

最新文章

  1. 人脸检测--Recurrent Scale Approximation for Object Detection in CNN
  2. 普通用户 fork报错 fork: retry: No child processes 解决方法
  3. CSS hack:针对IE6,IE7,IE8,IE9,firefox显示不同效果
  4. Android深度探索读后感第二章
  5. win8安装msi出现提示2503 2502的错误代码
  6. java 前端页面传过来的值怎么防止篡改_答对这40道经典web前端面试题,想不拿到offer都难!...
  7. 最长回文 HDU - 3068(求最长回文串的长度【马拉车算法Manacher】)
  8. MySQL抛出 Lock wait timeout exceeded; try restarting transaction
  9. vue 第八天 小结 作业模拟购物车
  10. 手工增加widcomm对蓝牙USB dongle的识别
  11. 未来互联网最稀缺的能力是什么?
  12. 算法:回溯十五 Palindrome Partitioning字符串的字符串为回文所有解集合(2中解法)
  13. Node.js菜鸟教程入门笔记摘要
  14. jetbrain试用
  15. 基金会要申请银行账户吗
  16. supermap mysql_SuperMap iMobile for Android定位实现
  17. 2022出海非洲:加纳电商市场现状及发展前景
  18. linux交叉编译 windows,从Windows到Linux的C ++交叉编译器
  19. 集合框架中的共性功能
  20. 梯度下降及python实现

热门文章

  1. 边缘和智能,是谁在借谁上位?
  2. 深度长文 | 复旦大学肖仰华:领域知识图谱落地实践中的问题与对策
  3. 叮!请查收本周份「知识图谱」专题论文解读
  4. Sklearn 损失函数如何应用到_Sklearn库主要模块功能简介
  5. sqlite to mysql_SqliteToMysql官方版
  6. Java中最早期的集合Vector
  7. The POM for com.ruifeng.tjtaxiqy:shiro:jar:0.0.1-SNAPSHOT is missing, no dependency information avai
  8. Little Sub and AA
  9. Magic Powder - 2
  10. maven(二)pom文件详解