packagecom.collections;importcom.google.common.collect.Lists;importjava.util.List;importjava.util.Objects;importjava.util.stream.Collectors;importjava.util.stream.IntStream;importstaticorg.junit.Assert.assertEquals;publicclassListsTest{publicstaticvoidmain(String[] args){List<Object> objectList =Lists.newArrayList();// testCartesianProduct();// testTransform();// testNewArrayListWithCapacity();// testNewArrayListWithExpectedSize();// testReverse();// testPartition();}privatestaticvoidtestPartition(){List<String> list =Lists.newArrayList("1","2","3","4");List<List<String>> partition =Lists.partition(list,2);List<String> list1 = partition.get(0);// 1 2List<String> list2 = partition.get(1);// 3 4}privatestaticvoidtestReverse(){List<Integer> list =Lists.newArrayList(1,2,3);List<Integer> reverseList =Lists.reverse(list);assertEquals(reverseList,Lists.newArrayList(3,2,1));}privatestaticvoidtestNewArrayListWithExpectedSize(){List<Integer> list =Lists.newArrayListWithExpectedSize(5);assertEquals(list.size(),0);}privatestaticvoidtestNewArrayListWithCapacity(){/**
* initialArraySize
*/List<Integer> list =Lists.newArrayListWithCapacity(2);assertEquals(list.size(),0);IntStream.rangeClosed(1,3).forEach(list::add);System.out.println(list);}privatestaticvoidtestTransform(){List<String> list =Lists.newArrayList(null,"Alex","Java","Python");List<String> transform = list.stream().filter(Objects::nonNull).map(String::toUpperCase).collect(Collectors.toList());System.out.println(transform);}/**
* 测试笛卡儿积
*/privatestaticvoidtestCartesianProduct(){List<List<String>> lists =Lists.cartesianProduct(Lists.newArrayList("1","2"),Lists.newArrayList("A","B"));System.out.println(lists);}}
2 Sets
packagecom.collections;importcom.google.common.collect.Lists;importcom.google.common.collect.Sets;importjava.util.List;importjava.util.Set;importstaticorg.junit.Assert.assertEquals;publicclassSetsTest{publicstaticvoidmain(String[] args){/**
* Set<Integer> set = new HashSet<Integer>() {{
* add(1);
* add(2);
* add(3);
* }};
*/// testNewHashSet();// testCartesianProduct();// testCombinations();// testDifference();// testIntersection();// testUnion();}privatestaticvoidtestUnion(){Sets.SetView<Integer> union =Sets.union(Sets.newHashSet(1,2,3),Sets.newHashSet(4));Set<Integer> set =Sets.newHashSet(union);System.out.println(set);}privatestaticvoidtestIntersection(){Sets.SetView<Integer> intersection =Sets.intersection(Sets.newHashSet(1,2,3),Sets.newHashSet(2));Set<Integer> set =Sets.newHashSet(intersection);System.out.println(set);}privatestaticvoidtestDifference(){Sets.SetView<Integer> difference =Sets.difference(Sets.newHashSet(1,2,3),Sets.newHashSet(2));Set<Integer> set =Sets.newHashSet(difference);System.out.println(set);// [1, 3]}privatestaticvoidtestCombinations(){/**
* Returns the set of all subsets of set of size size. For example,
* combinations(ImmutableSet.of(1, 2, 3), 2)
* returns the set {{1, 2}, {1, 3}, {2, 3}}.
*/Set<Set<Integer>> set =Sets.combinations(Sets.newHashSet(1,2,3),2);
set.forEach(System.out::println);/**
* [1, 2]
* [1, 3]
* [2, 3]
*/}privatestaticvoidtestCartesianProduct(){Set<List<Integer>> set =Sets.cartesianProduct(Sets.newHashSet(1,2,3,1),Sets.newHashSet(1,1,2));assertEquals(set.size(),6);System.out.println(set);// [[1, 1], [1, 2], [2, 1], [2, 2], [3, 1], [3, 2]]}privatestaticvoidtestNewHashSet(){assertEquals(Sets.newHashSet(1,2,3,1).size(),3);assertEquals(Sets.newHashSet(Lists.newArrayList(1,2,3,4,5,1)),Sets.newHashSet(1,2,3,4,5));}}
3 Maps
packagecom.collections;importcom.google.common.collect.ImmutableMap;importcom.google.common.collect.Lists;importcom.google.common.collect.Maps;importcom.google.common.collect.Sets;importjava.util.List;importjava.util.Map;importjava.util.Set;publicclassMapsTest{publicstaticvoidmain(String[] args){// uniqueIndex();// asMap();// transformValues();// filterKeys();}privatestaticvoidfilterKeys(){Set<String> set =Sets.newHashSet("name","age","gender");Map<String,String> map =Maps.asMap(set,String::toUpperCase);/**
* public static <K, V> Map<K, V> filterKeys(
* Map<K, V> unfiltered, final Predicate<? super K> keyPredicate) {}
*/Map<String,String> filterKeys =Maps.filterKeys(map,"name"::equals);System.out.println(filterKeys);}privatestaticvoidtransformValues(){Set<String> set =Sets.newHashSet("name","age","gender");Map<String,String> map =Maps.asMap(set,String::toUpperCase);/**
* public static <K, V1, V2> Map<K, V2> transformValues(
* Map<K, V1> fromMap, Function<? super V1, V2> function) {}
*/Map<String,String> transformValuesMap =Maps.transformValues(map, e -> e +"_VALUE");System.out.println(transformValuesMap);// {name=NAME_VALUE, gender=GENDER_VALUE, age=AGE_VALUE}}privatestaticvoidasMap(){Set<String> set =Sets.newHashSet("name","age","gender");/**
* public static <K, V> Map<K, V> asMap(Set<K> set, Function<? super K, V> function) {}
*/Map<String,String> map =Maps.asMap(set,String::toUpperCase);System.out.println(map);}privatestaticvoiduniqueIndex(){List<String> list =Lists.newArrayList("name","age","gender");/**
* A Map whose contents will never change, with many other important properties detailed at ImmutableCollection.
* public static <K, V> ImmutableMap<K, V> uniqueIndex(
* Iterable<V> values, Function<? super V, K> keyFunction) {}
*/ImmutableMap<String,String> immutableMap =Maps.uniqueIndex(list,String::toUpperCase);System.out.println(immutableMap);// {NAME=name, AGE=age, GENDER=gender}}}