Guava学习之Lists
发布日期:2021-06-30 11:32:32 浏览次数:2 分类:技术文章

本文共 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函数实现。更多的实现可以参看其源码。(完)

转载请注明: 转载自
本文链接地址: 

转载地址:https://iteblog.blog.csdn.net/article/details/11542363 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Kafka、Flink 数据中台实践:事件模型、调度、实时/离线数仓架构之道
下一篇:Apache Iceberg 你需要知道的原理与技术

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月30日 12时01分53秒