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. C++虚函数的缺陷

    MFC中的消息机制没有采用C++中的虚函数机制,原因是消息太多,虚函数内存开销太大.在Qt中也没有采用C++中的虚函数机制,原因与此相同,其实这里还有更深层次上的原因,大体说来,多态的底层实现机制只有 ...

  2. ActiveMQ简单的HelloWorld实例

    我们使用ActiveMQ为大家实现一种点对点的消息模型. 开发时候,要将apache-activemq-5.12.0-bin.zip解压缩后里面的activemq-all-5.12.0.jar包加入到 ...

  3. 306. Additive Number

    题目: Additive number is a string whose digits can form additive sequence. A valid additive sequence s ...

  4. minhash

    minhash是一种基于jaccard index 相似度的算法.属于LSH(Location Sensitive Hash)家族中的一员. jaccard index :有两个集合A={a , b ...

  5. Java 数据结构之Stack

    Stack类表示后进先出(LIFO)的对象堆栈.栈是一种非常常见的数据结构.Stack继承Vector,并对其进行了扩展. 用法: 1.只有一个构造函数: public Stack() {} 2.创建 ...

  6. USACO Section 3.1: Agri-Net

    minimal spanning tree的经典题 /* ID: yingzho1 LANG: C++ TASK: agrinet */ #include <iostream> #incl ...

  7. lib-qqwry v1.0 发布 nodejs解析纯真IP库(qqwry.dat)

    lib-qqwry是当初学习node时用来练手的一个模块,用来解析纯真IP库的 现在发一个v1.0版本弥补我当时稚嫩的代码. 意外收获是,整理代码后发现,相比v0.x版本 急速模式下的效率提升大概20 ...

  8. 每天一个小算法(Shell sort5)

    希尔排序的关键在于步长的选取. 希尔排序的复杂度比较复杂,主要跟步长的选择有关,大概是 O(n logn^2),一般认为就是介于 O(n^2) 和 O(n logn) 之间.最好步长比较复杂,一般第一 ...

  9. BZOJ 1507 Editor(块状链表)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1507 题意:一个文本编辑器,模拟以下操作: 思路:块状链表的主要操作: (1)find( ...

  10. Hadoop集群(第2期)_机器信息分布表

    1.分布式环境搭建 采用4台安装Linux环境的机器来构建一个小规模的分布式集群. 图1 集群的架构 其中有一台机器是Master节点,即名称节点,另外三台是Slaver节点,即数据节点.这四台机器彼 ...