guava集合类(二)


有经验的同学都知道,如果Abc是一个类,那么它没有实现的功能,可能会出现在Abcs类中。

比如Arrays是对数组功能的扩展,Collections是对集合功能的扩展。Files、Paths是对File、Path类相关操作的扩展。

guava由此延伸,构建了更多对原生集合的加强操作,都集中在Abcs类中。

一、构造

这些工具类中,首先会有基于构建者模式的静态构造方法。

InterfaceJDK or Guava?Corresponding Guava utility class
Collection JDK Collections2
List JDK Lists
Set JDK Sets
SortedSet JDK Sets
Map JDK Maps
SortedMap JDK Maps
Queue JDK Queues
Multiset Guava Multisets
Multimap Guava Multimaps
BiMap Guava Maps
Table Guava Tables

//
Iterables Iterable concatenated = Iterables.concat( Ints.asList(1, 2, 3), Ints.asList(4, 5, 6)); // concatenated has elements 1, 2, 3, 4, 5, 6 String lastAdded = Iterables.getLast(myLinkedHashSet); String theElement = Iterables.getOnlyElement(thisSetIsDefinitelyASingleton); //Sets Set wordsWithPrimeLength = ImmutableSet.of("one", "two", "three", "six", "seven", "eight"); Set primes = ImmutableSet.of("two", "three", "five", "seven"); SetView intersection = Sets.intersection(primes, wordsWithPrimeLength); //Lists List countDown = Lists.reverse(theList); // {5, 4, 3, 2, 1} List> parts = Lists.partition(countUp, 2); // {{1, 2}, {3, 4}, {5}} //Maps ImmutableMap stringsByIndex = Maps.uniqueIndex(strings, new Function () { public Integer apply(String string) { return string.length(); } }); Map left = ImmutableMap.of("a", 1, "b", 2, "c", 3); Map right = ImmutableMap.of("b", 2, "c", 4, "d", 5); MapDifference diff = Maps.difference(left, right); //Multisets Multiset multiset1 = HashMultiset.create(); multiset1.add("a", 2); Multiset multiset2 = HashMultiset.create(); multiset2.add("a", 5); multiset1.containsAll(multiset2); // returns true: all unique elements are contained, // even though multiset1.count("a") == 2 < multiset2.count("a") == 5 Multisets.containsOccurrences(multiset1, multiset2); // returns false Multisets.removeOccurrences(multiset2, multiset1); // multiset2 now contains 3 occurrences of "a" multiset2.removeAll(multiset1); // removes all occurrences of "a" from multiset2, even though multiset1.count("a") == 2 multiset2.isEmpty(); // returns true //Multimaps ImmutableSet digits = ImmutableSet.of( "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"); Function lengthFunction = new Function() { public Integer apply(String string) { return string.length(); } }; ImmutableListMultimap digitsByLength = Multimaps.index(digits, lengthFunction); //Tables Table table = Tables.newCustomTable( Maps.>newLinkedHashMap(), new Supplier> () { public Map get() { return Maps.newLinkedHashMap(); } });

二、反转

ArrayListMultimap multimap = ArrayListMultimap.create();
multimap.putAll("b", Ints.asList(2, 4, 6));
multimap.putAll("a", Ints.asList(4, 2, 1));
multimap.putAll("c", Ints.asList(2, 5, 3));

TreeMultimap inverse = Multimaps.invertFrom(multimap, TreeMultimap. create());
// note that we choose the implementation, so if we use a TreeMultimap, we get results in order
/*
 * inverse maps:
 *  1 => {"a"}
 *  2 => {"a", "b", "c"}
 *  3 => {"c"}
 *  4 => {"a", "b"}
 *  5 => {"c"}
 *  6 => {"b"}
 */


Map map = ImmutableMap.of("a", 1, "b", 1, "c", 2);
SetMultimap multimap = Multimaps.forMap(map);
// multimap maps ["a" => {1}, "b" => {1}, "c" => {2}]
Multimap inverse = Multimaps.invertFrom(multimap, HashMultimap. create());
// inverse maps [1 => {"a", "b"}, 2 => {"c"}]



ImmutableSet digits = ImmutableSet.of(
    "zero", "one", "two", "three", "four",
    "five", "six", "seven", "eight", "nine");
Function lengthFunction = new Function() {
  public Integer apply(String string) {
    return string.length();
  }
};
ImmutableListMultimap digitsByLength = Multimaps.index(digits, lengthFunction);
/*
 * digitsByLength maps:
 *  3 => {"one", "two", "six"}
 *  4 => {"zero", "four", "five", "nine"}
 *  5 => {"three", "seven", "eight"}
 */