Google Guava API学习笔记(2):集合

来源:互联网 发布:d3.js 地图 编辑:程序博客网 时间:2024/06/05 17:08
不可变集合 Immutable Collections 


例子 
Java代码  收藏代码
  1. public static final ImmutableSet<String> COLOR_NAMES = ImmutableSet.of(  
  2.   "red",  
  3.   "orange",  
  4.   "yellow",  
  5.   "green",  
  6.   "blue",  
  7.   "purple");  
  8.   
  9. class Foo {  
  10.   Set<Bar> bars;  
  11.   Foo(Set<Bar> bars) {  
  12.     this.bars = ImmutableSet.copyOf(bars); // defensive copy!  
  13.   }  
  14. }  


为啥? 

不可变对象拥有众多好处: 

  • Safe for use by untrusted libraries.
  • Thread-safe: can be used by many threads with no risk of race conditions.
  • Doesn't need to support mutation, and can make time and space savings with that assumption. All immutable collection implementations are more memory-efficient than their mutable siblings (analysis)
  • Can be used as a constant, with the expectation that it will remain fixed


防止可变对象拷贝是一个良好的编程习惯,请看这儿:http://www.javapractices.com/topic/TopicAction.do?Id=15 

Making immutable copies of objects is a good defensive programming technique. Guava provides simple, easy-to-use immutable versions of each standard Collection type, including Guava's own Collection variations. 

接下来,Guava说居然JDK的Colletions提供了一套Collections.unmodifiableXXX方法,但不太好用,因此Guava不是在重复造轮子。又强调说,如果你不希望对一个集合进行update,那么最好采用防御性拷贝方式,将它们拷贝到一个不可变的集合中。Guava的集合实现都不支持null的元素(Guva通过研究,发现95%的情况不需要使用null的集合元素),所以如果需要null集合元素,就不能用Guava了。 

咋整? 

可使用如下方法创建不可变集合: 
  • using the copyOf method, for example, ImmutableSet.copyOf(set)
  • using the of method, for example, ImmutableSet.of("a", "b", "c") or ImmutableMap.of("a", 1, "b", 2)
  • using a Builder, 看示例:


Java代码  收藏代码
  1. public static final ImmutableSet<Color> GOOGLE_COLORS =  
  2.        ImmutableSet.<Color>builder()  
  3.            .addAll(WEBSAFE_COLORS)  
  4.            .add(new Color(0191255))  
  5.            .build();  



使用copeOf(0 

Java代码  收藏代码
  1. ImmutableSet<String> foobar = ImmutableSet.of("foo""bar""baz");  
  2. thingamajig(foobar);  
  3.   
  4. void thingamajig(Collection<String> collection) {  
  5.    ImmutableList<String> defensiveCopy = ImmutableList.copyOf(collection);  
  6.    ...  
  7. }  


asList 

All immutable collections provide an ImmutableList view via asList(), so -- for example -- even if you have data stored as an ImmutableSortedSet, you can get the kth smallest element with sortedSet.asList().get(k). 

通过asList构造的List通常比直接new ArrayList要快,毕竟它是指定大小的。 


在哪? 

InterfaceJDK or Guava?Immutable VersionCollectionJDKImmutableCollectionListJDKImmutableListSetJDKImmutableSetSortedSet/NavigableSetJDKImmutableSortedSetMapJDKImmutableMapSortedMapJDKImmutableSortedMapMultisetGuavaImmutableMultisetSortedMultisetGuavaImmutableSortedMultisetMultimapGuavaImmutableMultimapListMultimapGuavaImmutableListMultimapSetMultimapGuavaImmutableSetMultimapBiMapGuavaImmutableBiMapClassToInstanceMapGuavaImmutableClassToInstanceMapTableGuavaImmutableTable

创建集合 

简化集合创建 
原来多麻烦: 
Java代码  收藏代码
  1. Map<String, Integer> counts = new HashMap<String, Integer>();  
  2. for (String word : words) {  
  3.   Integer count = counts.get(word);  
  4.   if (count == null) {  
  5.     counts.put(word, 1);  
  6.   } else {  
  7.     counts.put(word, count + 1);  
  8.   }  
  9. }  


现在: 

Java代码  收藏代码
  1. Multiset<String> wordsMultiset = HashMultiset.create();  
  2. wordsMultiset.addAll(words);  


Table 

Java代码  收藏代码
  1. Table<Vertex, Vertex, Double> weightedGraph = HashBasedTable.create();  
  2. weightedGraph.put(v1, v2, 4);  
  3. weightedGraph.put(v1, v3, 20);  
  4. weightedGraph.put(v2, v3, 5);  
  5.   
  6. weightedGraph.row(v1); // returns a Map mapping v2 to 4, v3 to 20  
  7. weightedGraph.column(v3); // returns a Map mapping v1 to 20, v2 to 5  


工具类 

InterfaceJDK or Guava?Corresponding Guava utility classCollectionJDKCollections2 (avoiding conflict with java.util.Collections)ListJDKListsSetJDKSetsSortedSetJDKSetsMapJDKMapsSortedMapJDKMapsQueueJDKQueuesMultisetGuavaMultisetsMultimapGuavaMultimapsBiMapGuavaMapsTableGuavaTables

JDK 7.0之前: 
Java代码  收藏代码
  1. List<TypeThatsTooLongForItsOwnGood> list = new ArrayList<TypeThatsTooLongForItsOwnGood>();  


Guava提供如下方式: 
Java代码  收藏代码
  1. List<TypeThatsTooLongForItsOwnGood> list = Lists.newArrayList();  
  2. Map<KeyType, LongishValueType> map = Maps.newLinkedHashMap();  


JDK 7.0才支持: 
Java代码  收藏代码
  1. List<TypeThatsTooLongForItsOwnGood> list = new ArrayList<>();  



Guava提供更多: 

Java代码  收藏代码
  1. Set<Type> copySet = Sets.newHashSet(elements);  
  2. List<String> theseElements = Lists.newArrayList("alpha""beta""gamma");  


指定大小: 
Java代码  收藏代码
  1. List<Type> exactly100 = Lists.newArrayListWithCapacity(100);  
  2. List<Type> approx100 = Lists.newArrayListWithExpectedSize(100);  
  3. Set<Type> approx100Set = Sets.newHashSetWithExpectedSize(100);  


类似的: 
Java代码  收藏代码
  1. Multiset<String> multiset = HashMultiset.create();  


对象工具类提供的方法,如: 

Java代码  收藏代码
  1. List<Integer> countUp = Ints.asList(12345);  
  2. List<Integer> countDown = Lists.reverse(theList); // {5, 4, 3, 2, 1}  
  3.   
  4. List<List<Integer>> parts = Lists.partition(countUp, 2); // {{1, 2}, {3, 4}, {5}}  



不可变工具类提供的方法,如: 
Java代码  收藏代码
  1. Set<String> wordsWithPrimeLength = ImmutableSet.of("one""two""three""six""seven""eight");  
  2. Set<String> primes = ImmutableSet.of("two""three""five""seven");  
  3.   
  4. SetView<String> intersection = Sets.intersection(primes, wordsWithPrimeLength); // contains "two", "three", "seven"  
  5. // I can use intersection as a Set directly, but copying it can be more efficient if I use it a lot.  
  6. return intersection.immutableCopy();  



扩展工具类 

略了,看:http://code.google.com/p/guava-libraries/wiki/CollectionHelpersExplained

转自:http://stamen.iteye.com/blog/1669120