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 ...
随机推荐
- Qt中的多线程技术(列表总结比较,多线程创建和销毁其实是有开销的,只是增加了用户体验而已)
http://blog.csdn.net/u011012932/article/details/52943811
- C++:文件的输入和输出
1.共同的打开文件方式: fin.open("test.txt",ios::binary) fout.open("test.txt",ios::binary) ...
- CodeForces485A——Factory(抽屉原理)
Factory One industrial factory is reforming working plan. The director suggested to set a mythical d ...
- Oracle ->> Oracle下生成序列的方法
用hierachical query,即connect by配合dual表生成序列,mod这个是取余函数,生成group factor.最后面的connect by rownum <= 100可 ...
- poj 1180 斜率优化dp
这个题目要是顺着dp的话很难做,但是倒着推就很容易退出比较简单的关系式了. dp[i]=min(dp[u]+(sum[u-1]-sum[i-1]+s)*f[i]);dp[i]代表从i到结尾需要花费的代 ...
- dojo 总结
对以前项目中用到的dojo框架进行一个框架式的总结,以备参考学习. 主要组成... 1 开发注意... 3 Dojo代码约定... 3 Dojo形式的脚本库... 4 Dojo Build. 4 Do ...
- (二)javascript中int和string转换
在javascript里怎么样才能把int型转换成string型 (1)var x=100 a = x.toString() (2)var x=100; a = x +""; // ...
- 创建下拉列表并通过ajax填充下拉数据
$(document).ready(function(e) { jQuery.post('${basePath}/customerService/getCustomerService4List.do' ...
- 如何从List<T>中筛选符合条件的数据的集合或个数
方法一:Linq ChannelList就是一个List类型的数据,IsOpen 是其元素的属性 channelCount = (from channel in DevicesManager.Inst ...
- Jquery-json
第三方插件: jquery.json-2.4.js Jquery-json 是 jQuery 的一个插件,可轻松实现对象和 JSON 字符串之间的转换.可序列化 JavaScript 对象.数值.字符 ...