public static void main(String[] args) {
Set<String> set1 = new HashSet<String>();
Set<String> set2 = new HashSet<String>();
set1.add("abc"); set2.add("abc");
set1.add("123"); set2.add("123");
set1.add("ABC"); set2.add("XYZ");
//此处注意,retainAll之后,set1的内容将会发生改变,改变为两个集合的交集内容,
//如不想set1的内容发生改变,则新建一个List保存
set1.retainAll(set2);
System.out.println("交集元素个数是:"+set1.size());
}本文出自 “架构师之路” 博客,请务必保留此出处http://lizhuquan0769.blog.51cto.com/2591147/1787961
原文地址:http://lizhuquan0769.blog.51cto.com/2591147/1787961