Lists

 List类主要提供了对List类的子类构造以及操作的静态方法。在类中支持构造ArrayList、LinkedList以及newCopyOnWriteArrayList对象的方法。
其中提供了以下构造ArrayList的函数:下面四个构造一个ArrayList对象,但是不显式的给出申请空间的大小:

1 newArrayList()
2 newArrayList(E... elements)
3 newArrayList(Iterable<? extends E> elements)
4 newArrayList(Iterator<? extends E> elements)

   以下两个函数在构造ArrayList对象的时候给出了需要分配空间的大小:

1    newArrayListWithCapacity(int initialArraySize)
2    newArrayListWithExpectedSize(int estimatedSize)

  如果你事先知道元素的个数,可以用newArrayListWithCapacity函数;如果你不能确定元素的个数,可以用newArrayListWithExpectedSize函数,在newArrayListWithExpectedSize函数里面调用了computeArrayListCapacity(int arraySize)函数,其实现如下:

1    @VisibleForTesting static int computeArrayListCapacity(int arraySize) {
2        checkArgument(arraySize >= 0);
3    
4        // TODO(kevinb): Figure out the right behavior, and document it
5        return Ints.saturatedCast(5L + arraySize + (arraySize / 10));
6      }

返回的容量大小为5L + arraySize + (arraySize / 10),当arraySize比较大的时候,给定大小和真正分配的容量之比为10/11。
   Lists类还支持构造LinkedList、newCopyOnWriteArrayList对象,其函数接口为:

1    newLinkedList()
2    newLinkedList(Iterable<? extends E> elements)
3    
4    newCopyOnWriteArrayList()
5    newCopyOnWriteArrayList(Iterable<? extends E> elements)

   我们还可以将两个(或三个)类型相同的数据存放在一个list中,这样可以传入到只有一个参数的函数或者需要减少参数的函数中,这些函数如下:

1    asList(@Nullable E first, E[] rest)
2    asList(@Nullable E first, @Nullable E second, E[] rest)

   Lists类中transform函数可以根据传进来的function对fromList进行相应的处理,并将处理得到的结果存入到新的list对象中,这样有利于我们进行分析,函数接口如下:

1    public static <F, T> List<T> transform(
2          List<F> fromList, Function<? super F, ? extends T> function)

   

使用例子:

 1         Function<String, Integer> strlen = new Function<String, Integer>() {
 2             public Integer apply(String from) {
 3                 Preconditions.checkNotNull(from);
 4                 return from.length();
 5             }
 6         };
 7         List<String> from = Lists.newArrayList("abc", "defg", "hijkl");
 8         List<Integer> to = Lists.transform(from, strlen);
 9         for (int i = 0; i < from.size(); i++) {
10             System.out.printf("%s has length %d
", from.get(i), to.get(i));
11         }
12 
13 
14         Function<String, Boolean> isPalindrome = new Function<String, Boolean>() {
15             public Boolean apply(String from) {
16                 Preconditions.checkNotNull(from);
17                 return new StringBuilder(from).reverse().toString().equals(from);
18             }
19         };
20         from = Lists.newArrayList("rotor", "radar", "hannah", "level", "botox");
21         List<Boolean> to1 = Lists.transform(from, isPalindrome);
22         for (int i = 0; i < from.size(); i++) {
23             System.out.printf("%s is%sa palindrome
", from.get(i), to1.get(i) ? " " : " NOT ");
24         }
25         // changes in the "from" list are reflected in the "to" list
26         System.out.printf("
now replace hannah with megan...

");
27         from.set(2, "megan");
28         for (int i = 0; i < from.size(); i++) {
29             System.out.printf("%s is%sa palindrome
", from.get(i), to1.get(i) ? " " : " NOT ");
30         }

Lists还可以将传进来的String或者CharSequence分割为单个的字符,并存入到一个新的List对象中返回,如下:

1 ImmutableList<Character> wyp = Lists.charactersOf("wyp");
2 System.out.println(wyp);

将List对象里面的数据顺序反转可以用reverse函数实现,取得List对象里面的子序列可以用subList函数实现。更多的实现可以参看其源码。

原文地址:https://www.cnblogs.com/lijia0511/p/5738422.html