当前位置: 移动技术网 > IT编程>开发语言>Java > Java提取2个集合中的相同和不同元素代码示例

Java提取2个集合中的相同和不同元素代码示例

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文分享的示例代码实现提取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]

总结

以上就是本文关于java提取2个集合中的相同和不同元素代码示例的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网