Java提取2个集合中的相同和不同元素代码示例
作者:weixin_38824230 发布时间:2023-11-28 05:48:41
标签:java,集合,元素
本文分享的示例代码实现提取2个集合中相同和不同的元素
此处需要使用Collection集合所提供的一个方法:removeAll(Cellection list),removeAll方法用于从列表中移除指定collection中包含的所有元素。
语法 removeAll(Collection<?> c)
c:包含从列表中移除元素的collection对象。
该方法返回值为boolean对象,如果List集合对象由于调用removeAll方法而发生更改,则返回true,否则返回false。实现代码如下:
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Test {
public static void main(String args[]){
//集合一
List _first=new ArrayList();
_first.add("jim");
_first.add("tom");
_first.add("jack");
//集合二
List _second=new ArrayList();
_second.add("jack");
_second.add("happy");
_second.add("sun");
_second.add("good");
Collection exists=new ArrayList(_second);
Collection notexists=new ArrayList(_second);
exists.removeAll(_first);
System.out.println("_second中不存在于_set中的:"+exists);
notexists.removeAll(exists);
System.out.println("_second中存在于_set中的:"+notexists);
}
}
运行结果:
_second中不存在于_set中的:[happy, sun, good]
_second中存在于_set中的:[jack]
来源:https://www.2cto.com/kf/201711/694758.html
0
投稿
猜你喜欢
- 一、直接看效果二、直接上代码1.自定义控件部分package com.susan.project.myapplication;import
- 自动生成的代码报错解决办法:把自动xml文件中自动生成的二级缓存注释掉来源:https://blog.csdn.net/weixin_447
- 发现问题最近发现在mybatis中如果使用的字段是Oracle的关键字,会出现错误,通过查找相关的资料终于解决了,下面来一起看看详细的解决方
- 本文实例讲述了Android基于TextView实现的跑马灯效果。分享给大家供大家参考,具体如下:package sweet.venst.a
- 1. 引入jar包pom.xml文件<?xml version="1.0" encoding="UTF-
- 前言我在以往的文章中曾介绍过如何给Word文档添加文本水印和图片水印,及怎样删除文档中的水印。关于文本水印,之前那篇教程里主要指的是单行字体
- int -> String int i=12345;String s="";核心:s=i+""
- spring boot2集成activiti6踩过的坑1.activiti中的mybaitis版本冲突 错误信息Caused by: jav
- 十年前,Java 还是计算机科学的入门课程中的必学语言。如果你想学其他语言,比如 C、Python、PHP,你就得专门选那个语言的课程,或者
- 近期,公司推行正版化,本人使用的是JetBrains教育版,是不允许进行商业开发的,因此开启了艰难的备用IDE选型之路。最终,我选定了轻量级
- 1.在C#中使用FolderBrowserDialog类,就可以实现选择文件夹的功能,并将所选择的的文件夹路径记录下来。(1).首先先引入命
- 1. 缓存、两级缓存1.1 内容说明Spring cache:主要包含spring cache定义的接口方法说明和注解中的属性说明sprin
- 首先,要学习Spring中的Bean的注入方式,就要先了解什么是依赖注入。依赖注入是指:让调用类对某一接口的实现类的实现类的依赖关系由第三方
- 转成 Base64 形式的 System.String:string a = "base64字符串与普通字符串互转";
- 参考内容:深入理解Java虚拟机(JVM高级特性与最佳实践) ——周志明老师尚硅谷深入理解JVM教学视频——宋红康老师在本文展开前,读者需要
- 本文实例为大家分享了android实现点击按钮控制图片切换的具体代码,供大家参考,具体内容如下代码:class MainActivity :
- 上帝之火本系列讲述的是开源实时监控告警解决方案Prometheus,这个单词很牛逼。每次我都能联想到带来上帝之火的希腊之神,普罗米修斯。而这
- springboot Interceptor * excludePathPatterns忽略失效excludePathPatterns方法是
- 对于Android开发者来说虽然使用了可以自动管理内存的Java语言,但是对于内存管理不当,可能你的应用不断出现类似INFO/dalvikv
- Application对象Application对象生存期和Web应用程序生存期一样长,生存期从Web应用程序网页被访问开始,HttpApp