Lists (Guava: Google Core Libraries for Java 27.0.1-jre API)https://guava.dev/releases/27.0.1-jre/api/docs/com/google/common/collect/Lists.html对应于List集合接口, 在com.google.common.collect包下
Lists 接口的声明如下:
@GwtCompatible(emulated=true)public final class Lists extends Object
@VisibleForTesting static int computeArrayListCapacity(int arraySize) { checkArgument(arraySize >= 0); // TODO(kevinb): Figure out the right behavior, and document it return Ints.saturatedCast(5L + arraySize + (arraySize / 10));}
asList(@Nullable E first, E[] rest)asList(@Nullable E first, @Nullable E second, E[] rest)
String str = "i love u"; String[] strs = {"i like u", "i miss u"}; List strings = Lists.asList(str, strs); System.out.println(strings);
Lists 类中 transform 函数可以根据传进来的 function 对 fromList 进行相应的处理,并将处理得到的结果存入到新的list对象中,这样有利于我们进行分析,函数接口如下:
public static List transform(List fromList, Function<? super F, ? extends T> function)
Function strlen = new Function() { public Integer apply(String from) { Preconditions.checkNotNull(from); return from.length(); } }; List from = Lists.newArrayList("abc", "defg", "hijkl"); List to = Lists.transform(from, strlen); for (int i = 0; i < from.size(); i++) { System.out.printf("%s has length %dn", from.get(i), to.get(i)); }
Maps
官方文档
Maps (Guava: Google Core Libraries for Java 27.0.1-jre API)https://guava.dev/releases/27.0.1-jre/api/docs/com/google/common/collect/Maps.html com.google.common.collect.Maps 接口的声明:
@GwtCompatible(emulated=true)public final class Maps extends Object
Sets (Guava: Google Core Libraries for Java 27.0.1-jre API)https://guava.dev/releases/27.0.1-jre/api/docs/com/google/common/collect/Sets.htmlcom.google.common.collect.Sets 接口的声明:
@GwtCompatible(emulated=true)public final class Sets extends Object
@Test public void testFilter() { Set set = Sets.newHashSet("i like u", "i miss u", "i love u"); Predicate predicate = new Predicate() { @Override public boolean apply(String input) { //过滤包含字母l的元素 return input.contains("l"); } }; System.out.println(Sets.filter(set, predicate)); // [i like u, i love u] System.out.println(Sets.filter(set, input -> input.contains("l"))); // [i like u, i love u] }
difference
@Test public void testDifference() { Set set1 = Sets.newHashSet(1, 2, 3, 4, 5); Set set2 = Sets.newHashSet(1, 3, 5, 7, 9); System.out.println(Sets.difference(set1, set2)); // [2, 4] }
symmetricDifference
@Test public void testSymmetricDifference() { Set set1 = Sets.newHashSet(1, 2, 3, 4, 5); Set set2 = Sets.newHashSet(1, 3, 5, 7, 9); System.out.println(Sets.symmetricDifference(set1, set2)); // [2, 4, 9, 7] }
intersection
@Test public void testIntersection() { Set set1 = Sets.newHashSet(1, 2, 3, 4, 5); Set set2 = Sets.newHashSet(1, 3, 5, 7, 9); System.out.println(Sets.intersection(set1, set2)); // [1, 3, 5] }
@Test public void testCartesianProduct() { Set set1 = Sets.newHashSet("i love u", "i hate u"); Set set2 = Sets.newHashSet("tom", "jerry"); Set> sets = Sets.cartesianProduct(set1, set2); System.out.println(sets); // [[i hate u, tom], [i hate u, jerry], [i love u, tom], [i love u, jerry]] }
powerSet
@Test public void testPowerSet() { Set set1 = Sets.newHashSet("A", "B", "C"); Set> sets = Sets.powerSet(set1);// for (Set set : sets) {// System.out.println(set);// } System.out.println(Arrays.toString(sets.toArray())); // [[], [A], [B], [A, B], [C], [A, C], [B, C], [A, B, C]] }
完整代码
maven项目里用junit进行单元测试
import com.google.common.base.Predicate;import com.google.common.collect.Sets;import org.junit.Test;import java.util.Arrays;import java.util.List;import java.util.Set;public class TestSets { @Test public void testFilter() { Set set = Sets.newHashSet("i like u", "i miss u", "i love u"); Predicate predicate = new Predicate() { @Override public boolean apply(String input) { //过滤包含字母l的元素 return input.contains("l"); } }; System.out.println(Sets.filter(set, predicate)); // [i like u, i love u] System.out.println(Sets.filter(set, input -> input.contains("l"))); // [i like u, i love u] } @Test public void testDifference() { Set set1 = Sets.newHashSet(1, 2, 3, 4, 5); Set set2 = Sets.newHashSet(1, 3, 5, 7, 9); System.out.println(Sets.difference(set1, set2)); // [2, 4] } @Test public void testSymmetricDifference() { Set set1 = Sets.newHashSet(1, 2, 3, 4, 5); Set set2 = Sets.newHashSet(1, 3, 5, 7, 9); System.out.println(Sets.symmetricDifference(set1, set2)); // [2, 4, 9, 7] } @Test public void testIntersection() { Set set1 = Sets.newHashSet(1, 2, 3, 4, 5); Set set2 = Sets.newHashSet(1, 3, 5, 7, 9); System.out.println(Sets.intersection(set1, set2)); // [1, 3, 5] } @Test public void testUnion() { Set set1 = Sets.newHashSet(1, 2, 3, 4, 5); Set set2 = Sets.newHashSet(1, 3, 5, 7, 9); System.out.println(Sets.union(set1, set2)); // [1, 2, 3, 4, 5, 9, 7] } @Test public void testCartesianProduct() { Set set1 = Sets.newHashSet("i love u", "i hate u"); Set set2 = Sets.newHashSet("tom", "jerry"); Set> sets = Sets.cartesianProduct(set1, set2); System.out.println(sets); // [[i hate u, tom], [i hate u, jerry], [i love u, tom], [i love u, jerry]] } @Test public void testPowerSet() { Set set1 = Sets.newHashSet("A", "B", "C"); Set> sets = Sets.powerSet(set1);// for (Set set : sets) {// System.out.println(set);// } System.out.println(Arrays.toString(sets.toArray())); // [[], [A], [B], [A, B], [C], [A, C], [B, C], [A, B, C]] }}