【发布时间】:2014-02-28 13:45:12
【问题描述】:
我浏览了一些谷歌制作的JAVA代码,找到了ImmutableSet:http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/ImmutableSet.html
他们用其他几种方式实现了 of() 方法:
public static <E> ImmutableSet<E> of(E e1, E e2);
public static <E> ImmutableSet<E> of(E e1, E e2, E e3);
public static <E> ImmutableSet<E> of(E e1, E e2, E e3, E e4);
public static <E> ImmutableSet<E> of(E e1, E e2, E e3, E e4, E e5);
public static <E> ImmutableSet<E> of(E... elements);
有一个带有以下签名的 create 方法:
private static <E> ImmutableSet<E> create(E... elements)
包装了
private static <E> ImmutableSet<E> create(Iterable<? extends E> iterable, int count);
方法。公共方法只是将参数传递给 create(E... elements) 签名方法,该方法最终调用另一个 create 方法。
我猜想,由于我们有 of(E... elements) 方法,所以没有必要公开具有固定参数数量的方法。
我的问题是,他们为什么要这样做?表现?还是一种模式?
谢谢。
【问题讨论】:
-
旁注:如果您使用的是旧的 Google 收藏库,请不要使用,它不再受支持。使用新的闪亮的Google Guava,它取代了谷歌收藏(并且正在积极开发)。
标签: java performance theory