List类主要提供了对List类的子类构造以及操作的静态方法。在类中支持构造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函数实现。更多的实现可以参看其源码。

Lists的更多相关文章

  1. [LeetCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  2. [LeetCode] Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...

  3. [LeetCode] Merge Two Sorted Lists 混合插入有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  4. 21. Merge Two Sorted Lists —— Python

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  5. 【leetcode】Intersection of Two Linked Lists

    题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  6. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...

  7. [LintCode] Merge Two Sorted Lists 混合插入有序链表

    Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list sh ...

  8. ubuntu 解决 “E: Problem with MergeList /var/lib/apt/lists/”错误

    这种错误的意思:无法解析或打开软件包的列表或是状态文件. 出现的原因:无法解析或打开软件包列表多数情况是安装的软件与本身系统有一些冲突之类的问题,或者曾在更新软件源或下载软件的时候意外中断造成的. 解 ...

  9. No.023:Merge k Sorted Lists

    问题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...

  10. No.021:Merge Two Sorted Lists

    问题: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

随机推荐

  1. 让ie6也支持max-width,和max-height实现图片等比例缩放

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. Java-数据结构与算法-逢3减1

    1.要求:有一群人围成一圈数数,逢3退1人,要求算出最后留下来的人的下标 2.代码: package Test; public class Count3Quit1 { //要求:有一群人围成一圈数数, ...

  3. Linux命令(3):wc命令

    Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的字节数. ...

  4. c# 文件简繁体转换

    C#   文件简繁体转换 简繁体转换: 方案一: 准确性高,耗性能 方案二: 准确性低,效率高 1 using Microsoft.International.Converters.Tradition ...

  5. Java异常 —— java.lang.NoClassDefFoundError

    一直使用 Eclipse 来开发 Java . 现学习 Maven,在 cmd 下使用 Java ,出现了这样的异常:Exception in thread "main" java ...

  6. python中的类简单讲解

    类似其它的语言, Python 中的函数使用小括号( () )调用.函数在调用之前必须先定义.如果函数中没有 return 语句, 就会自动返回 None 对象.      Python 是通过引用调 ...

  7. enable c++11 in autoconf in fucking gnu auto tools

    configure.ac => CXXFLAGS="$CXXFLAGS -std=c++14" set CXXFLAGS  => std=c++14 well done ...

  8. 随便谈谈alphago与人机大战

    3月16日历时8天的人机大战终于落下帷幕,alphago以4:1的比分击败了当年如日中天的李世石.这个结果让我这个围棋爱好者+计算机爱好者百感交集…… ——一个时代落幕了,一个新的时代开启了. 这次人 ...

  9. 【第四篇】说说layer的iframe弹窗给里面的标签赋值的问题

    说到这一篇,真的是颠覆了我的思维. 官方文档,没有介绍这一部分的操作,大致上提了一下. 我的思路是把页面的数据传过去,在iframe弹窗的页面拿到接收到的数据,然后赋值,但是这样就会有个问题, 怎么传 ...

  10. 基于HtmlUnit的模板的网页数据抽取

    既然方向定了,就开始做实验室吧,做舆情分析,首先就是要收集相关的语料 正好实验室有同学在做标化院的信息抽取抽取这块 于是把程序拿过来研究研究正好 完整程序在126邮箱共享: 可下载数:20  共享连接 ...