Apache Common 包学习:常用集合类Collections4学习

Apache Common 包学习:常用集合类Collections4学习

Maven依赖

1
2
3
4
5
6
7
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.3</version>
</dependency>
</dependencies>

CollectionUtils

<O> Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b)

a是做差集运算的左集,b是做差集运算的右集,下面是一个例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Demo {

private static final Logger LOGGER = LoggerFactory.getLogger(Demo.class);

public static void main(String[] args) {

Set<Pair<String, String>> allProductDevices = Sets.newHashSet(Pair.of("a", "b"), Pair.of("c", "d"));

Set<Pair<String, String>> oldProductDevices = Sets.newHashSet(Pair.of("a", "b"), Pair.of("e", "f"));

List<Pair<String, String>> newProductDevices =
(ArrayList<Pair<String, String>>) CollectionUtils.subtract(allProductDevices, oldProductDevices);

List<String> list1 = Lists.newArrayList("a", "b");

List<String> list2 = Lists.newArrayList("c", "b");

List<String> list3 = (ArrayList<String>) CollectionUtils.subtract(list1, list2);
}
}