Guava笔记Lists
来源:互联网 发布:3ds数据搬家 编辑:程序博客网 时间:2024/06/05 06:12
Guava笔记Lists
省掉泛型的写法
static <E> ArrayList<E> newArrayList()
Creates a mutable, empty ArrayList instance (for Java 6 and earlier).
在jdk7中已经进步了一点,不用写具体的泛型之类。List<String> list = Lists.newArrayList();
返回String字符串的list
static ImmutableList<Character> charactersOf(String string)
Returns a view of the specified string as an immutable list of Character values.
返回等分List
static <T> List<List<T>> partition(List<T> list, int size)
Returns consecutive sublists of a list, each of the same size (the final list may be smaller). 返回一个等分数组,最后一个可能比较少
反序List
- static List reverse(List list)
Returns a reversed view of the specified list.
这个虽然是view,但是却可以返回可操作的list。支持被反序list的操作,比如add,并且如果对这个view做了修改,也会影响到原来的list,并以反序的方式增加进去
重点:transform
static <F,T> List<T> transform(List<F> fromList, Function<? super F,? extends T> function)
Returns a list that applies function to each element of fromList.
这个只看api的话,有点难看明白,还是看一个具体的例子吧。Function<String, Integer> func = new Function<String, Integer>() { @Override public Integer apply(String str) { return str.length(); } };List<String> strList = Arrays.asList(new String[]{"你", "我", "他"});List<Integer> list = Lists.transform(strList, func);for (int i : list ) { System.out.println(i);}
简单来说,就是写一个匿名的function实现,实现每个元素从F到T的类型转换。之后就可以应用了。还是比较喜欢这个function的。
Note:基于function的转换是lazy的,并且返回的是一个view。如果修改了src的list或者view的list都会反馈到src List。目前的情况是,我使用Lists.transform改变了src list中的element的属性,然后返回到具体view上就是修改了两次的结果。这个也可以期待为正常的结果,但是有时候也出现修改三次的结果。两次的结果可以理解,但是三次就不能很理解了。希望有知道的,可以告诉我具体的执行过程。
0 0
- Guava笔记Lists
- Guava学习之Lists
- Guava ---- Lists列表
- Guava collections -- Lists
- Guava学习之Lists
- guava使用Lists.partition,Lists.transform小结
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- Guava Lists.transform踩坑小记
- guava 源码之 Lists transform的坑
- Guava(一)Lists与Maps
- Guava Lists.transform 使用及问题
- 最熟悉的陌生人:ListView 中的观察者模式
- 构建shadowsocks服务器并局域网共享
- ECMAScript6笔记:Iterator和for...of循环
- centos之lnmp
- HashSet源码阅读
- Guava笔记Lists
- Java RMI 框架(远程方法调用)
- 敏捷开发系列学习总结(8)——创业公司研发团队建设
- 关于SVN提交不成功问题
- MySQL——存储过程和函数使用总结
- SAP新总账 凭证分割
- 公式编辑器编辑双向斜箭头的教程
- pthread_cleanup_push()/pthread_cleanup_pop()
- USB设备,鼠标,键盘使用CH372,CH375,进行模拟的历程