博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Guava Collections API学习之Lists
阅读量:4150 次
发布时间:2019-05-25

本文共 2938 字,大约阅读时间需要 9 分钟。

 

类主要提供了对List类的子类构造以及操作的静态方法。在类中支持构造ArrayList、LinkedList以及newCopyOnWriteArrayList对象的方法。

其中提供了以下构造ArrayList的函数:下面四个构造一个ArrayList对象,但是不显式的给出申请空间的大小:

 

newArrayList()newArrayList(E... elements)newArrayList(Iterable
elements)newArrayList(Iterator
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
elements)   newCopyOnWriteArrayList()newCopyOnWriteArrayList(Iterable
elements)

 

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

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

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

public static 
List
transform(List
fromList, Function
function)

 使用例子:

Function
strlen = new Function
() { public Integer apply(String from) { Preconditions.checkNotNull(from); return from.length(); } }; List
from = Lists.newArrayList("abc", "defg", "hijkl"); List
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
isPalindrome = new Function
() { 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
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
wyp = Lists.charactersOf("wyp");System.out.println(wyp);

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

 

 

转载地址:http://splti.baihongyu.com/

你可能感兴趣的文章
背包问题代码
查看>>
poj1276
查看>>
最少硬币问题
查看>>
C++STL算法
查看>>
poj3211
查看>>
用eclipse导入新工程
查看>>
查找有向图中的环
查看>>
windows下配置java
查看>>
vs2008 c++ 调用java
查看>>
c++调用Java以及string互转
查看>>
c++dll生成和调用
查看>>
jni详解
查看>>
反向代理
查看>>
解决Error spawning 'cmd.exe'的问题
查看>>
biset传参
查看>>
不能断点调试
查看>>
c++最大最小值
查看>>
强制转换const 引用
查看>>
poj1273 最大流
查看>>
算法分类
查看>>