JAVA中区分2个集合中的相同和不同元素

来源:互联网 发布:python 3.6.2 和2.7 编辑:程序博客网 时间:2024/06/06 15:39

找到2个集合中相同和不同的元素

此处需要使用Collection集合所提供的一个方法:removeAll(Cellection list)

实现代码如下:

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Test {
public  static void main(String args[]){
//集合一
List<String> _first=new ArrayList<String>();
_first.add("jim");
_first.add("tom");
_first.add("jack");
//集合二
List<String> _second=new ArrayList<String>();
_second.add("jack");
_second.add("happy");
_second.add("sun");
_second.add("good");

Collection exists=new ArrayList<String>(_second);
Collection notexists=new ArrayList<String>(_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]

阅读全文
0 0
原创粉丝点击