Java List 获取部分组成new list,获取两个list相同/不同的内容

server/2024/10/22 18:30:09/

获取List中的一项,组成新的List<Object>

List<String> _outingCntry = list.stream().map(OSzItem::getGroup).
collect(Collectors.toList());

获取List中的多项,组成一个Map(Item1,item2)

List<vo> list = new ArrayList<>();
Map<String, String> map = list.stream().collect(
Collectors.toMap(vo::getxxx, vo::getxxx));

 按照条件获取List,包括相同项,不同项等

获取两个Array / List 等不相同项是比较繁琐的过程,通常需要设置全局变量,然后通过for语句判断是否相等,同时改变全局变量。全局变量没有变化的项即是不同。

这种方式显得代码啰嗦,不符合自然思考习惯,所有可以借助List的stream来解决,代码相对整洁。

List<SalrCntry> _diff = _amtCntrylist.stream().
filter(ele->!_outCntry.contains(ele.getCntry())).
collect(Collectors.toList());

通过remove去除相同,剩下的是不同 

public class FindDifferencesBetweenListsUnitTest { private static final List listOne = Arrays.asList("Jack", "Tom", "Sam", "John", "James", "Jack"); private static final List listTwo = Arrays.asList("Jack", "Daniel", "Sam", "Alan", "James", "George"); }

List<String> differences = new ArrayList<>(listOne); differences.removeAll(listTwo); assertEquals(2, differences.size()); assertThat(differences).containsExactly("Tom", "John");

List<String> differences = listTwo.stream() .filter(element -> !listOne.contains(element)) .collect(Collectors.toList()); assertEquals(3, differences.size()); assertThat(differences).containsExactly("Daniel", "Alan", "George");


http://www.ppmy.cn/server/30806.html

相关文章

nvm pnpm powershell

nvm 下载 在 nvm 安装路径下修改 settings.txt root: e:\xxx\nvm path: e:\xxx\nodejs npm_mirror https://npmmirror.com/mirrors/npm/ node_mirror https://npmmirror.com/mirrors/node/nvm list available nvm install 18.20.2 nvm use 18.20.2npm config list npm config …

ServiceNow 研究:通过RAG减少结构化输出中的幻觉

论文地址&#xff1a;https://arxiv.org/pdf/2404.08189 原文地址&#xff1a;rag-hallucination-structure-research-by-servicenow 在灾难性遗忘和模型漂移中&#xff0c;幻觉仍然是一个挑战。 2024 年 4 月 18 日 灾难性遗忘&#xff1a; 这是在序列学习或连续学习环境中出现…

启发式搜索算法1 - 最佳优先搜索算法

启发式搜索算法有什么优势&#xff1f; 对于复杂问题的盲目搜索&#xff0c;常用广度优先搜索和深度优先搜索这两种盲目搜索算法&#xff0c;极大极小值和Alpha-beta剪枝算法是在盲目搜索过程中&#xff0c;通过剪枝避开一些不可能的结果&#xff0c;从而提高效率。 如果搜索…

华为OD机试 - 会议室占用时间段(Java 2024 C卷 100分)

华为OD机试 2024C卷题库疯狂收录中&#xff0c;刷题点这里 专栏导读 本专栏收录于《华为OD机试&#xff08;JAVA&#xff09;真题&#xff08;A卷B卷C卷&#xff09;》。 刷的越多&#xff0c;抽中的概率越大&#xff0c;每一题都有详细的答题思路、详细的代码注释、样例测试…

Nacos、OpenFeign、网关 笔记

一、远程调用 1.1配置RestTemplate配置类 package com.hmall.cart.config;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate;Configuration public c…

Python之字符串,列表,元组,字典之间的转换写法

# 转换函数# 元组与列表之间的转换# 把元组转换成列表 # list() lis1 list(tuple1) print(lis1) # [成龙, 小玉, 老爹, 特鲁, 瓦龙, 布莱特, 圣主]# 把列表转换成元组 # tuple() tup1 tuple(lis1) print(tup1) # (成龙, 小玉, 老爹, 特鲁, 瓦龙, 布莱特, 圣主)# 字符串与列表…

【研发日记】Matlab/Simulink避坑指南(十一)——Delay周期Bug

文章目录 前言 背景介绍 问题描述 分析排查 解决方案 总结归纳 前言 见《研发日记&#xff0c;Matlab/Simulink避坑指南(六)——字节分割Bug》 见《研发日记&#xff0c;Matlab/Simulink避坑指南(七)——数据溢出钳位Bug》 见《研发日记&#xff0c;Matlab/Simulink避坑指…

【Java EE】Mybatis之XML详解

文章目录 &#x1f38d;配置数据库连接和MyBatis&#x1f340;写持久层代码&#x1f338;添加mapper接口&#x1f338;添加UserInfoXMLMapper.xml&#x1f338;单元测试 &#x1f332;CRUD&#x1f338;增(Insert)&#x1f338;删(Delete)&#x1f338;改(Update)&#x1f338;…