maven引入:
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
废话不多说,下面直接上代码
/**
* difference(Set<? extends E> a, Set<? extends E> b)
* Returns a unmodifiable view containing the difference of the given Sets, denoted by a \ b (or a - b).
*/
@Test
public void test1() {
Set<String> set1 = new HashSet<>();
set1.add("a");
set1.add("b");
set1.add("c");
System.out.println("set1 = " + set1); // set1 = [a, b, c]
Set<String> set2 = new HashSet<>();
set2.add("b");
set2.add("c");
set2.add("d");
System.out.println("set2 = " + set2); // set2 = [b, c, d]
SetUtils.SetView<String> result = SetUtils.difference(set1, set2);
System.out.println("result = " + result); // result = [a]
System.out.println("set1 = " + set1); // set1 = [a, b, c]
System.out.println("set2 = " + set2); // set2 = [b, c, d]
}
/**
* disjunction(Set<? extends E> a, Set<? extends E> b)
* Returns a unmodifiable view of the symmetric difference of the given Sets.
* This is equivalent to union(difference(a, b), difference(b, a)).
*/
@Test
public void test2() {
Set<String> a = new HashSet<>(Arrays.asList("a", "b", "c", "d", "e"));
Set<String> b = new HashSet<>(Arrays.asList("c", "d", "e", "f", "g"));
SetUtils.SetView<String> result = SetUtils.disjunction(a, b);
System.out.println("result = " + result); // result = [a, b, f, g]
a.add("k");
System.out.println("result = " + result); // result = [a, b, k, f, g]
}
/**
* emptyIfNull(Set<T> set)
* Returns an immutable empty set if the argument is null, or the argument itself otherwise.
*/
@Test
public void test3() {
// case 1:
Set<String> set = null;
Set<String> result = SetUtils.emptyIfNull(set);
System.out.println("result = " + result); // result = []
result.add("name");
System.out.println("result = " + result); // java.lang.UnsupportedOperationException
}
/**
* emptySet()
* Get a typed empty unmodifiable Set.
*/
@Test
public void test4() {
Set<String> set = SetUtils.emptySet();
System.out.println("set = " + set); // set = []
set.add("name");
System.out.println("set = " + set); // java.lang.UnsupportedOperationException
}
/**
* emptySortedSet()
* Get a typed empty unmodifiable sorted set.
*/
@Test
public void test5() {
SortedSet<Object> set = SetUtils.emptySortedSet();
System.out.println("set = " + set); // set = []
// set.add("name");
// System.out.println("set = " + set); // java.lang.UnsupportedOperationException
HashSet<String> hashSet = new HashSet<>();
hashSet.add("name");
hashSet.add("age");
hashSet.add("sex");
System.out.println("hashSet = " + hashSet); // hashSet = [sex, name, age]
SortedSet<String> sortedSet = new TreeSet<>();
sortedSet.add("name");
sortedSet.add("age");
sortedSet.add("sex");
System.out.println("sortedSet = " + sortedSet); // sortedSet = [age, name, sex]
}
/**
* hashCodeForSet(Collection<T> set)
* Generates a hash code using the algorithm specified in Set.hashCode().
*/
@Test
public void test6() {
Set<String> set = new HashSet<>(Arrays.asList("a", "b", "c", "d", "e", "f"));
int result = SetUtils.hashCodeForSet(set);
System.out.println("result = " + result); // result = 597
}
/**
* hashSet(E... items)
* Creates a set from the given items.
*/
@Test
public void test7() {
HashSet<String> set = SetUtils.hashSet("a", "b", "c", "d", "e", "f");
int hashCode = SetUtils.hashCodeForSet(set);
System.out.println("hashCode = " + hashCode); // hashCode = 597
}
/**
* intersection(Set<? extends E> a, Set<? extends E> b)
* Returns a unmodifiable view of the intersection of the given Sets.
* 取交集
*/
@Test
public void test8() {
HashSet<String> setA = SetUtils.hashSet("a", "b", "c", "d");
HashSet<String> setB = SetUtils.hashSet("a", "c", "g", "h");
SetUtils.SetView<String> result = SetUtils.intersection(setA, setB);
System.out.println("result = " + result); // result = [a, c]
}
/**
* isEqualSet(Collection<?> set1, Collection<?> set2)
* Tests two sets for equality as per the equals() contract in Set.equals(java.lang.Object).
* 底层用containsAll方式实现
* 判断两个集合是否相等,跟元素顺序无关,就算是equals方法也一样
*/
@Test
public void test9() {
HashSet<String> setA = SetUtils.hashSet("name", "age", "sex");
HashSet<String> setB = SetUtils.hashSet("name", "age", "sex");
boolean isEqual = SetUtils.isEqualSet(setA, setB);
System.out.println("setA = " + setA); // setA = [sex, name, age]
System.out.println("setB = " + setB); // setB = [sex, name, age]
System.out.println("isEqual = " + isEqual); // isEqual = true
isEqual = setA.equals(setB);
System.out.println("isEqual = " + isEqual); // isEqual = true
setB = SetUtils.hashSet("name", "sex", "age");
isEqual = SetUtils.isEqualSet(setA, setB);
System.out.println("setA = " + setA); // setA = [sex, name, age]
System.out.println("setB = " + setB); // setB = [sex, name, age]
System.out.println("isEqual = " + isEqual); // isEqual = true
isEqual = setA.equals(setB);
System.out.println("isEqual = " + isEqual); // isEqual = true
Set<String> setC = new TreeSet<>(Arrays.asList("name", "age", "sex"));
System.out.println("setC = " + setC); // setC = [age, name, sex]
boolean result = SetUtils.isEqualSet(setA, setC);
System.out.println("result = " + result); // result = true
result = setA.equals(setC);
System.out.println("result = " + result); // result = true
}
/**
* newIdentityHashSet()
* Returns a new hash set that matches elements based on == not equals().
* 加入的对象如果用等于号(==)进行比较为true,则判定为同一个元素;而不是通过equals方法来判断。
* 等于号(==)比较的是对象地址,即用System.identityHashCode(obj)获得的值
*/
@Test
public void test10() {
// case 1:
Set<String> set = SetUtils.newIdentityHashSet();
set.add("zheng");
set.add("hui");
set.add("xiang");
System.out.println("set = " + set); // set = [hui, xiang, zheng]
set.add("zheng");
System.out.println("set = " + set); // set = [hui, xiang, zheng]
// case 2:
Set<String> set2 = SetUtils.newIdentityHashSet();
set2.add(new String("hui"));
set2.add(new String("zheng"));
set2.add(new String("zheng"));
System.out.println("set2 = " + set2); // set2 = [zheng, hui, zheng]
// case 3:
Set<String> set3 = new HashSet<>();
set3.add(new String("hui"));
set3.add(new String("zheng"));
set3.add(new String("zheng"));
System.out.println("set3 = " + set3); // set3 = [zheng, hui]
}
/**
* orderedSet(Set<E> set)
* Returns a set that maintains the order of elements that are added backed by the given set.
*/
@Test
public void test11() {
Set<String> set = SetUtils.orderedSet(new HashSet<>());
set.add("zheng");
set.add("hui");
set.add("xiang");
System.out.println("set = " + set); // set = [zheng, hui, xiang]
}
/**
* predicatedNavigableSet(NavigableSet<E> set, Predicate<? super E> predicate)
* Returns a predicated (validating) navigable set backed by the given navigable set.
*/
@Test
public void test12() {
Predicate<String> predicate = p -> p.length() == 5;
NavigableSet<String> set = new TreeSet<>();
SortedSet<String> result = SetUtils.predicatedNavigableSet(set, predicate);
result.add("zheng");
System.out.println("result = " + result + "; set = " + set); // result = [zheng]; set = [zheng]
result.add("huixi");
System.out.println("result = " + result + "; set = " + set); // result = [huixi, zheng]; set = [huixi, zheng]
result.add("zheng");
System.out.println("result = " + result + "; set = " + set); // result = [huixi, zheng]; set = [huixi, zheng]
}
/**
* predicatedSet(Set<E> set, Predicate<? super E> predicate)
* Returns a predicated (validating) set backed by the given set.
*/
@Test
public void test13() {
Set<String> set = SetUtils.predicatedSet(new HashSet<String>(), e -> e.length() >= 3);
set.add("zhx");
System.out.println("set = " + set); // set = [zhx]
set.add("zx"); // java.lang.IllegalArgumentException: Cannot add Object 'zx' - Predicate 'com.iwhale.www.common.SetUtilsTest$$Lambda$1/474675244@69222c14' rejected it
}
/**
* predicatedSortedSet(SortedSet<E> set, Predicate<? super E> predicate)
* Returns a predicated (validating) sorted set backed by the given sorted set.
*/
@Test
public void test14() {
SortedSet<String> set = SetUtils.predicatedSortedSet(new TreeSet<>(), e -> e.length() >= 3);
set.add("zex");
System.out.println("set = " + set);
}
/**
* transformedSet(Set<E> set, Transformer<? super E,? extends E> transformer)
* Returns a transformed set backed by the given set.
* Existing entries in the specified set will not be transformed.
*/
@Test
public void test15() {
Set<String> set = SetUtils.transformedSet(new HashSet<>(), e -> e.concat("-transformed"));
set.add("zheng");
set.add("hui");
set.add("xiang");
System.out.println("set = " + set); // set = [xiang-transformed, hui-transformed, zheng-transformed]
}
/**
* union(Set<? extends E> a, Set<? extends E> b)
* Returns a unmodifiable view of the union of the given Sets.
* 合并之后是无序的
*/
@Test
public void test16() {
Set<String> setA = new HashSet<>();
Set<String> setB = new TreeSet<>();
setA.add("zheng");
setA.add("hui");
setA.add("xiang");
System.out.println("setA = " + setA); // setA = [xiang, zheng, hui]
setB.add("zheng");
setB.add("hui");
setB.add("xiang");
System.out.println("setB = " + setB); // setB = [hui, xiang, zheng]
SetUtils.SetView<String> result = SetUtils.union(setA, setB);
System.out.println("result = " + result); // result = [xiang, zheng, hui]
}
/**
* unmodifiableSet(E... items)
* Creates an unmodifiable set from the given items.
*/
@Test
public void test17() {
Set<String> set = SetUtils.unmodifiableSet("zheng", "hui", "xiang");
System.out.println("set = " + set); // set = [xiang, zheng, hui]
set.add("zheng"); // java.lang.UnsupportedOperationException
}
/**
* unmodifiableSet(Set<? extends E> set)
* Returns an unmodifiable set backed by the given set.
*/
@Test
public void test18() {
Set<String> set = new HashSet<>(Arrays.asList("zheng", "hui", "xiang"));
System.out.println("set = " + set); // set = [xiang, zheng, hui]
Set<String> result = SetUtils.unmodifiableSet(set);
System.out.println("result = " + result); // result = [xiang, zheng, hui]
result.add("good"); // java.lang.UnsupportedOperationException
}