Lists
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的更多相关文章
- [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 ...
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- [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 ...
- 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 ...
- 【leetcode】Intersection of Two Linked Lists
题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- [LintCode] Merge Two Sorted Lists 混合插入有序链表
Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list sh ...
- ubuntu 解决 “E: Problem with MergeList /var/lib/apt/lists/”错误
这种错误的意思:无法解析或打开软件包的列表或是状态文件. 出现的原因:无法解析或打开软件包列表多数情况是安装的软件与本身系统有一些冲突之类的问题,或者曾在更新软件源或下载软件的时候意外中断造成的. 解 ...
- No.023:Merge k Sorted Lists
问题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
- 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 ...
随机推荐
- mysql 常用命令(备忘)
1:使用SHOW语句找出在服务器上当前存在什么数据库: mysql> SHOW DATABASES; 2:2.创建一个数据库MYSQLDATA mysql> CREATE DATABASE ...
- CentOS禁用root本地或远程ssh登录
有些特殊的情况我们需要禁止root在本地或远程使用ssh登录,以增加安全性. 禁止root本地登录 修改/etc/pam.d/login文件增加下面一行auth required pam_succee ...
- Java:字符串类String的功能介绍
在java中,字符串是一个比较常用的类,因为代码中基本上处理的很多数据都是字符串类型的,因此,掌握字符串类的具体用法显得很重要了. 它的主要功能有如下几种:获取.判断.转换.替换.切割.字串的获取.大 ...
- Linux内核通杀提权漏洞CVE-2016-5195 - 内核升级方法
如题,对于脏牛(Dirty COW)漏洞的修复方式已经在上篇文章中有介绍过如何验证,这里对如何升级内核给出修复建议. (注意:为避免不必要的生产风险的发生,请审核自己的实际环境而决定采用什么方法进行升 ...
- O(1)时间内删除指定链表结点
题目 给定单链表头指针和一个结点指针,定义一个函数在O(1)时间内删除该结点. 分析 对于上图实例链表(a)删除指针p有两种方式 思路1:(b)找到前一个指针pre,赋值pre->next = ...
- Docker基础技术:Linux Namespace(下)
在 Docker基础技术:Linux Namespace(上篇)中我们了解了,UTD.IPC.PID.Mount 四个namespace,我们模仿Docker做了一个相当相当山寨的镜像.在这一篇中,主 ...
- HDU4675【GCD of scequence】【组合数学、费马小定理、取模】
看题解一开始还有地方不理解,果然是我的组合数学思维比较差 然后理解了之后自己敲了一个果断TLE.... 我以后果然还得多练啊 好巧妙的思路啊 知识1: 对于除法取模还需要用到费马小定理: a ^ (p ...
- Ubuntu14.04安装和配置Tomcat8.0.12
Ubuntu14.04长的好看,所以一时间很感兴趣,研究各种软件的安装和开发环境的配置.今天先把安装的tomcat 8.0.12的教程分享给大家.如果你需要,请收藏!!! 官方网站下载最新的tom ...
- poj-3040 Allowance (贪心)
http://poj.org/problem?id=3040 FJ 有n种不同面值的硬币,每种硬币都有相应的个数,大面值的硬币值总能被小面值的硬币值整除,每周需要支付 Bessie c元,问最多能 ...
- 使用 httpkit 来替代 jetty
Compojure 是一个基于 ring 的上层web开发框架.在 lein new compojure my-app 生成的项目中,默认是启用 jetty 服务器的,最近用到了 http-kit 中 ...