1. 集合操作
import java.util.ArrayList;
import java.util.Collection;
import org.apache.commons.collections.CollectionUtils;
import com.google.common.collect.Lists;
public class CollectionUtilsIntro {
public static void main(String[] args) {
ArrayList<String> array1 = Lists.newArrayList("a", "b", "c");
ArrayList<String> array2 = Lists.newArrayList("c", "d", "e");
//并集
Collection<String> union = CollectionUtils.union(array1, array2);
//交集
Collection<String> intersection = CollectionUtils.intersection(array1, array2);
//交集的补集
Collection<String> disjunction = CollectionUtils.disjunction(array1, array2);
//集合相减
Collection<String> subtract = CollectionUtils.subtract(array1, array2);
System.out.println("并集:" + union);
System.out.println("交集" + intersection);
System.out.println("交集的补集" + disjunction);
System.out.println("集合相减" + subtract);
}
}
执行结果:
并集:[a, b, c, d, e]
交集:[c]
交集的补集:[a, b, d, e]
集合相减:[a, b]
2. 分片操作
import java.util.ArrayList;
import com.google.common.collect.Lists;
public class CollectionUtilsIntro {
public static void main(String[] args) {
ArrayList<String> array1 = Lists.newArrayList("a", "b", "c");
//分成了两个集合,没一个集合又可以单独的去处理。
Lists.partition(array1, 2).forEach(r -> {
System.out.println(r);
System.out.println("----");
});
}
}
执行结果:
[a, b]
----
[c]
----
网友评论