网站排名搜索,企业网站免费认证,北京网站建设要多少钱,网络购物系统任何具有JDK Collections Framework经验的程序员都知道并喜欢java.util.Collections.Guava提供了更多的实用程序#xff1a;适用于所有集合的静态方法。这些是番石榴最受欢迎和成熟的部分。 对应于特定接口的方法以相对直观的方式分组#xff1a; nterface JDK or Guava? …任何具有JDK Collections Framework经验的程序员都知道并喜欢java.util.Collections.Guava提供了更多的实用程序适用于所有集合的静态方法。这些是番石榴最受欢迎和成熟的部分。 对应于特定接口的方法以相对直观的方式分组 nterface JDK 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
一、静态构造函数
在JDK 7之前构建新的泛型集合需要令人不愉快的代码复制
ListTypeThatsTooLongForItsOwnGood list new ArrayListTypeThatsTooLongForItsOwnGood();
我想我们都可以同意这是令人不愉快的。Guava提供了使用泛型来推断右侧类型的静态方法
ListTypeThatsTooLongForItsOwnGood list Lists.newArrayList();
MapKeyType, LongishValueType map Maps.newLinkedHashMap();
可以肯定的是JDK 7中的菱形操作符减少了这方面的麻烦
ListTypeThatsTooLongForItsOwnGood list new ArrayList();
但Guava走得更远。使用工厂方法模式我们可以非常方便地用集合的起始元素来初始化集合。
SetType copySet Sets.newHashSet(elements);
ListString theseElements Lists.newArrayList(alpha, beta, gamma);
此外通过命名工厂方法有效Java项1我们可以提高将集合初始化为大小的可读性
ListType exactly100 Lists.newArrayListWithCapacity(100);
ListType approx100 Lists.newArrayListWithExpectedSize(100);
SetType approx100Set Sets.newHashSetWithExpectedSize(100);
下面列出了提供的精确静态工厂方法及其相应的实用程序类。 注意Guava引入的新集合类型不公开原始构造函数或者在实用程序类中具有初始值设定项。相反它们直接公开静态工厂方法例如
MultisetString multiset HashMultiset.create();
二、Iterables
只要可能Guava更喜欢提供接受Iterable而不是Collection的实用程序。在谷歌遇到一个“集合”并不罕见它实际上没有存储在主存中而是从数据库或另一个数据中心收集的并且在不实际获取所有元素的情况下无法支持size等操作。 因此您可能希望看到的所有集合都支持的许多操作都可以在Iterables中找到。此外大多数Iterables方法在iterator中都有一个接受原始迭代器的相应版本。 Iterables类中的绝大多数操作都是懒惰的它们只在绝对必要的时候推进支持迭代。返回Iterables的方法返回延迟计算的视图而不是在内存中显式地构造集合。 从Guava 12开始Iterables由FluentInterable类补充该类封装了Iterable并为其中许多操作提供了“流畅”的语法。 以下是最常用的实用程序的选择尽管在Guava函数习语中讨论了Iterables中许多更“函数”的方法。
1、一般使用 Method Description See Also concat(IterableIterable) 返回几个可迭代项的串联的惰性视图。 concat(Iterable...) frequency(Iterable, Object) 返回对象的出现次数。 Compare Collections.frequency(Collection, Object); see Multiset partition(Iterable, int) 返回一个不可修改的可迭代视图该视图被划分为指定大小的块。 Lists.partition(List, int), paddedPartition(Iterable, int) getFirst(Iterable, T default) 返回可迭代项的第一个元素如果为空则返回默认值。 Compare Iterable.iterator().next(), FluentIterable.first() getLast(Iterable) 返回可迭代项的最后一个元素如果它为空则会以NoSuchElementException快速失败。 getLast(Iterable, T default), FluentIterable.last() elementsEqual(Iterable, Iterable) 如果可迭代项具有相同顺序的相同元素则返回true。 Compare List.equals(Object) unmodifiableIterable(Iterable) 返回可迭代项的不可修改视图。 Compare Collections.unmodifiableCollection(Collection) limit(Iterable, int) 返回一个Iterable最多返回指定数量的元素。 FluentIterable.limit(int) getOnlyElement(Iterable) 返回Iterable中唯一的元素。如果可迭代项为空或具有多个元素则快速失败。 getOnlyElement(Iterable, T default)
IterableInteger concatenated Iterables.concat(Ints.asList(1, 2, 3),Ints.asList(4, 5, 6));
// concatenated has elements 1, 2, 3, 4, 5, 6String lastAdded Iterables.getLast(myLinkedHashSet);String theElement Iterables.getOnlyElement(thisSetIsDefinitelyASingleton);// if this set isnt a singleton, something is wrong!