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

本文共 2999 字,大约阅读时间需要 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函数实现。更多的实现可以参看其源码。(完)

转载请注明: 转载自
本文链接地址: 
你可能感兴趣的文章
记一件小事
查看>>
史上最烂项目:苦撑12年,600多万行代码...
查看>>
斯坦福后空翻机器人设计、代码全开源,成本降至3000美元,人人皆可DIY
查看>>
请停止学习框架
查看>>
考研比惨五大专业排行榜,第一名没人不服!
查看>>
没钱没公司,怎么做一款付费产品
查看>>
Python 3.8 新特性来袭
查看>>
查询亿级数据毫秒级返回!Elasticsearch 是如何做到的?
查看>>
FastAPI 构建 API 服务,究竟有多快?
查看>>
为什么Quora选择用Python语言?
查看>>
一劳永逸学编程的方法
查看>>
终于来了!Python 编辑神器 Jupyter ,推出首款官方可视化 Debug 工具!
查看>>
代码整洁之道-编写 Pythonic 代码
查看>>
如何科学的刷 Leetcode
查看>>
树莓派程序开机自启动
查看>>
连锁门店无线通信方案
查看>>
配置Lotus Domino集群视频详解
查看>>
通过PXE安装Linux实况
查看>>
轻松掌握Ubuntu Linux的3D桌面快捷键使用
查看>>
OSSIM(开源安全信息管理系统)在企业网络管理中的应用
查看>>