本问介绍了Guava中Sets集合类的一般使用情况, 例如集合的互斥、 交集、 并集等...
package com.wenniuwuren.collections;
import java.util.Iterator;
import java.util.Set;
import com.google.common.collect.Sets;
/**
* 对Sets工具类的使用
* @author wenniuwuren
*
*/
public class SetsTest {
public static void main(String[] args) {
/**
* 返回在s1中存在, 但不再s2中存在的
*/
Set<String> s1 = Sets.newHashSet("1", "2", "3");
Set<String> s2 = Sets.newHashSet("2", "3", "4");
System.out.println(Sets.difference(s1, s2));
/**
* 返回两个集合互斥集合
*/
System.out.println(Sets.symmetricDifference(s1, s2));
/**
* 返回两个集合的交集
*/
System.out.println(Sets.intersection(s1, s2));
/**
* 返回两个集合的并集
*/
System.out.println(Sets.union(s1, s2));
}
}
参考资料 :
《Getting Started with Google Guava》