[Lists](http://www.iteblog.com/archives/tag/lists "查看 Lists 中的全部文章")類主要提供了對List類的子類構造以及操作的靜態方法。在[Lists](http://www.iteblog.com/archives/tag/lists "查看 Lists 中的全部文章")類中支持構造ArrayList、LinkedList以及newCopyOnWriteArrayList對象的方法。
其中提供了以下構造ArrayList的函數:下面四個構造一個ArrayList對象,但是不顯式的給出申請空間的大小:
~~~
newArrayList()
newArrayList(E... elements)
newArrayList(Iterable<? extends E> elements)
newArrayList(Iterator<? extends E> elements)
~~~
以下兩個函數在構造ArrayList對象的時候給出了需要分配空間的大小:
~~~
newArrayListWithCapacity(int initialArraySize)
newArrayListWithExpectedSize(int estimatedSize)
~~~
如果你事先知道元素的個數,可以用newArrayListWithCapacity函數;如果你不能確定元素的個數,可以用newArrayListWithExpectedSize函數,在newArrayListWithExpectedSize函數里面調用了computeArrayListCapacity(int arraySize)函數,其實現如下:
~~~
@VisibleForTesting static int computeArrayListCapacity(int arraySize) {
checkArgument(arraySize >= 0);
// TODO(kevinb): Figure out the right behavior, and document it
return Ints.saturatedCast(5L + arraySize + (arraySize / 10));
}
~~~
返回的容量大小為5L + arraySize + (arraySize / 10),當arraySize比較大的時候,給定大小和真正分配的容量之比為10/11。
Lists類還支持構造LinkedList、newCopyOnWriteArrayList對象,其函數接口為:
~~~
newLinkedList()
newLinkedList(Iterable<? extends E> elements)
newCopyOnWriteArrayList()
newCopyOnWriteArrayList(Iterable<? extends E> elements)
~~~
我們還可以將兩個(或三個)類型相同的數據存放在一個list中,這樣可以傳入到只有一個參數的函數或者需要減少參數的函數中,這些函數如下:
~~~
asList(@Nullable E first, E[] rest)
asList(@Nullable E first, @Nullable E second, E[] rest)
~~~
Lists類中transform函數可以根據傳進來的function對fromList進行相應的處理,并將處理得到的結果存入到新的list對象中,這樣有利于我們進行分析,函數接口如下:
~~~
public static <F, T> List<T> transform(
List<F> fromList, Function<? super F, ? extends T> function)
~~~
使用例子:
~~~
Function<String, Integer> strlen = new Function<String, Integer>() {
public Integer apply(String from) {
Preconditions.checkNotNull(from);
return from.length();
}
};
List<String> from = Lists.newArrayList("abc", "defg", "hijkl");
List<Integer> to = Lists.transform(from, strlen);
for (int i = 0; i < from.size(); i++) {
System.out.printf("%s has length %d\n", from.get(i), to.get(i));
}
Function<String, Boolean> isPalindrome = new Function<String, Boolean>() {
public Boolean apply(String from) {
Preconditions.checkNotNull(from);
return new StringBuilder(from).reverse().toString().equals(from);
}
};
from = Lists.newArrayList("rotor", "radar", "hannah", "level", "botox");
List<Boolean> to1 = Lists.transform(from, isPalindrome);
for (int i = 0; i < from.size(); i++) {
System.out.printf("%s is%sa palindrome\n", from.get(i), to1.get(i) ? " " : " NOT ");
}
// changes in the "from" list are reflected in the "to" list
System.out.printf("\nnow replace hannah with megan...\n\n");
from.set(2, "megan");
for (int i = 0; i < from.size(); i++) {
System.out.printf("%s is%sa palindrome\n", from.get(i), to1.get(i) ? " " : " NOT ");
}
~~~
Lists還可以將傳進來的String或者CharSequence分割為單個的字符,并存入到一個新的List對象中返回,如下:
~~~
ImmutableList<Character> wyp = Lists.charactersOf("wyp");
System.out.println(wyp);
~~~
將List對象里面的數據順序反轉可以用reverse函數實現,取得List對象里面的子序列可以用subList函數實現。更多的實現可以參看其源碼。(完)
轉載請注明: 轉載自[過往記憶(http://www.iteblog.com/)](http://www.iteblog.com/)
本文鏈接地址:?[Guava學習之Lists(http://www.iteblog.com/archives/689)](http://www.iteblog.com/archives/689)