java使用stream判断两个list元素的属性并输出方式
作者:enjoy嚣士 发布时间:2023-02-06 14:15:18
标签:stream,list元素,属性,输出
使用stream判断两个list元素的属性并输出
/**
* 使用stream判断两个list中元素不同的item
*/
@Test
public void test1(){
List<Param> stringList1 = new LinkedList<Param>(){{
add(new Param(1,"1111"));
add(new Param(2, "2222"));
add(new Param(3, "3333"));
}};
List<Param> stringList2 = new LinkedList<Param>(){{
add(new Param(1,"1111"));
add(new Param(2, "4444"));
add(new Param(3, "5555"));
}};
// 判断key相同,value相同的元素
Map<Integer, String> tmpList2 = stringList2.stream().collect(Collectors.toMap(Param::getId, Param::getName));
var tmplist = stringList1.stream().filter(item -> (tmpList2.get(item.getId()) != null && tmpList2.get(item.getId()).equals(item.getName()))).collect(Collectors.toList());
System.out.println(tmplist);
}
@Setter
@Getter
@ToString
@AllArgsConstructor
public static class Param{
private Integer id;
private String name;
}
/**
* 使用stream判断两个list中元素不同的item
*/
@Test
public void test1(){
List<Param> stringList1 = new LinkedList<Param>(){{
add(new Param(1,"1111", "b"));
add(new Param(2, "2222", "c"));
add(new Param(3, "3333", "a"));
}};
List<Param> stringList2 = new LinkedList<Param>(){{
add(new Param(1,"1111", "c"));
add(new Param(2, "4444", "b"));
add(new Param(3, "5555", "a"));
}};
// 判断key相同,value相同的元素
Map<Integer, String> tmpList2 = stringList2.stream().collect(Collectors.toMap(Param::getId, Param::getName));
var tmplist = stringList1.stream().filter(item -> (tmpList2.get(item.getId()) != null && tmpList2.get(item.getId()).equals(item.getName()))).collect(Collectors.toList());
System.out.println(tmplist);
// 如果需要判断多个值,直接将对象加入进去
Map<Integer, Param> tmpList3 = stringList2.stream().collect(Collectors.toMap(Param::getId, Function.identity()));
var tmplist2 = stringList1.stream().filter(item -> (tmpList3.get(item.getId()) != null && tmpList3.get(item.getId()).getType().equals(item.getType()))).collect(Collectors.toList());
System.out.println(tmplist2);
}
@Setter
@Getter
@ToString
@AllArgsConstructor
@EqualsAndHashCode
public static class Param{
private Integer id;
private String name;
private String type;
}
stream判断列表是否包含某几个元素/重复元素
(需求经过修改过)判断一个profile是否包含PROFILE-IN-A和PROFILE-IN-B且都是Enable=1打勾的.
既然已经JDK8了,那就用lambda吧,如果是foreach可能比较难处理,用stream的filter则可以这样做.
核心代码可以这么写
int intCheck = profileServiceDtoList.stream().filter(e ->
"1".equals(e.getEnable())
&&(("PROFILE-IN-MOSHOW".equals(e.getServiceIdentifier()))||("PROFILE-IN-ADC".equals(e.getServiceIdentifier())))
).collect(Collectors.toList()).size();
代码SHOW
新建三个不同类型的profile,其中两个是要判断的,一个是干扰的.
通过steam进行filter,找出是否包含这两个元素(相当于把要判断的元素过滤进去)
判断list的size大小(intCheck>1找到两个则代表同时出现)
public static void main(String[] args) {
List<ProfileServiceDto> profileServiceDtoList= new ArrayList<>(3);
ProfileServiceDto profileService1 = new ProfileServiceDto();
profileService1.setServiceId(1001L);
profileService1.setServiceIdentifier("PROFILE-IN-MOSHOW");
profileService1.setEnable("1");
profileServiceDtoList.add(profileService1);
ProfileServiceDto profileService2 = new ProfileServiceDto();
profileService2.setServiceId(1002L);
profileService2.setServiceIdentifier("PROFILE-IN-ADC");
profileService2.setEnable("1");
profileServiceDtoList.add(profileService2);
ProfileServiceDto profileService3 = new ProfileServiceDto();
profileService3.setServiceId(1003L);
profileService3.setServiceIdentifier("PROFILE-XXX-ABC");
profileService3.setEnable("1");
profileServiceDtoList.add(profileService3);
int intCheck = profileServiceDtoList.stream().filter(e ->
"1".equals(e.getEnable())&&(("PROFILE-IN-MOSHOW".equals(e.getServiceIdentifier()))||("PROFILE-IN-ADC".equals(e.getServiceIdentifier())))
).collect(Collectors.toList()).size();
System.out.println("intCheck->"+intCheck);
if(intCheck>1){
System.error.println("In one profile, cannot contain two more PROFILE-IN profile.");
}
}
Java stream判断列表是否包含重复元素
思路是通过一个distinct的list,然后跟原先的list来判断大小,如果不一致(原先list.size>distinctList.size)则表示有重复元素
//profileServiceDtoList路上,不累赘
//多了一个profileService1.setGroupId("A");profileService1.setGroupId("B");profileService3.setGroupId("A");
List<String> groupList = new ArrayList<>(4);
profileServiceDtoList.stream().forEach(e -> {
if ("Y".equals(e.getEnable()) && StringUtils.isNotEmpty(e.getGroupId())) {
groupList.add(e.getGroupId());
}
});
int distinctGroupSize = groupList.stream().distinct().collect(Collectors.toList()).size();
if (groupList.size() > distinctGroupSize) {
throw new ValidationException("100001","In one profile, the services with the same groupId cannot co-exist.");
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
来源:https://enjoy-day.blog.csdn.net/article/details/116456140


猜你喜欢
- 我们都知道,Android上的界面展示都是通过Activity实现的,Activity实在是太常用了,我相信大家都已经非常熟悉了,这里就不再
- 效果图:布局文件里包括了一个ImageView和两个Button控件,而在MainActivity了初始化控件。<?xml versi
- 对数组使用 foreachC#提供 foreach 语句。 该语句提供一种简单、明了的方法来循环访问数组或任何可枚举集合的元素。 forea
- WebFlux服务编排WebFlux 服务编排是指使用 WebFlux 框架来编排多个异步服务的执行顺序和数据流动,从而构建出一个完整的、基
- 本文实例为大家分享了Android自定义加载圈动画展示的具体代码,供大家参考,具体内容如下实现如下效果: 该效果图主要有3个动画: 1.旋转
- Settings是WebView提供给上层App的一个配置Webview的接口,每个WebView都有一个WebSettings,要控制We
- 首先在新建了一个web服务文件。public SqlWhhWebService1() &nbs
- 前言反射和注解在java中偏高级用法,一般在各种框架中被广泛应用,文章简单介绍下反射和注解的用法,希望对你的工作学习有一定帮助java注解什
- 导入生成器需要的依赖坐标:<dependency> <groupId>com.baomidou</
- 最近在做一个项目,遇到了项目打成 war 包的一个问题,项目创建时选择的时 jar 包方式,后因项目部署要求,需要打成 war 包部署,遇到
- 前言如何下载和使用MyBatis Generator 插件,只说代码,不讲感情。如果有问题还请多多指点。开发环境开发工具:IntelliJ
- 今天查看登录日志,发现http_x_forwarded_for获取到的ip地址有些是内网ip地址,有些则是公网和内网ip地址一起获取到,用逗
- 本文实例为大家分享了Android实现记住账号密码的具体代码,供大家参考,具体内容如下布局一个复选框<CheckBox
- 本文实例讲述了C#获取项目指定目录下文件的方法。分享给大家供大家参考。具体如下:public List<FileInfo> Ge
- 在学习和回顾该知识前,已经掌握了if语句的结构和用法。if (条件) 语句;当条件满足的情况下,if结构
- 本文需要解决的问题笔者最近正在做一个项目,里面需要用到 Android Notification 机制来实现某些特定需求。我正好通过这个机会
- maven依赖及一些配置这里主要是搭建项目常用到的maven依赖以及搭建项目会需要用到的一些配置文件,可能下面这些依赖还不是很全,但是应该会
- 最近做了一个功能,里面涉及到了渐变圆形的需求。就是一个颜色可以渐变的圆环,最后实现的效果如下图:左图是带渐变效果,右图是不带渐变效果。原理还
- 某次,在大型的工程代码中,我这样调用:#include <iostream>using namespace std;namesp
- 在C#里关于定时器类就有3个 1.定义在System.Windows.Forms里 2.定义在System.Thre