/**
* Returns a list that applies {@code function} to each element of {@code
* fromList}. The returned list is a transformed view of {@code fromList};
* changes to {@code fromList} will be reflected in the returned list and vice
* versa.
*
* <p>Since functions are not reversible, the transform is one-way and new
* items cannot be stored in the returned list. The {@code add},
* {@code addAll} and {@code set} methods are unsupported in the returned
* list.  --对List元素的修改也不支持
*
* <p>The function is applied lazily, invoked when needed. This is necessary
* for the returned list to be a view, but it means that the function will be
* applied many times for bulk operations like {@link List#contains} and
* {@link List#hashCode}. For this to perform well, {@code function} should be
* fast. To avoid lazy evaluation when the returned list doesn't need to be a
* view, copy the returned list into a new list of your choosing. --如果returnList不需要追踪fromList的变化,可以copy returnList to a new List
*
* <p>If {@code fromList} implements {@link RandomAccess}, so will the
* returned list. The returned list is threadsafe if the supplied list and
* function are.
*
* <p>If only a {@code Collection} or {@code Iterable} input is available, use
* {@link Collections2#transform} or {@link Iterables#transform}.
*
* <p><b>Note:</b> serializing the returned list is implemented by serializing
* {@code fromList}, its contents, and {@code function} -- <i>not</i> by
* serializing the transformed values. This can lead to surprising behavior,
* so serializing the returned list is <b>not recommended</b>. Instead,
* copy the list using {@link ImmutableList#copyOf(Collection)} (for example),
* then serialize the copy. Other methods similar to this do not implement
* serialization at all for this reason.
*/

public class ListReference {

    public static void main(String[] args) {
List<UserEntity> userEntities = Lists.newArrayList();
userEntities.add(new UserEntity(1, "x"));
userEntities.add(new UserEntity(2, "y"));
List<UserDto> userDtos = Lists.transform(userEntities, new Function<UserEntity, UserDto>() {
public UserDto apply(UserEntity userEntity) {
return new UserDto(userEntity.getId(), userEntity.getName());
}
}); print(userDtos.iterator(), "before change return list... ");
testReference(userDtos);
print(userDtos.iterator(), "after change return list... "); for (UserEntity userEntity: userEntities) {
userEntity.setName(userEntity.getName() + "_change");
} print(userDtos.iterator(), "after change from list... ");
} private static void testReference(List<UserDto> userDtos) {
System.out.println("changing return list... ");
for (UserDto userDto: userDtos) {
userDto.setId(userDto.getId() + 100);
System.out.println(ToStringBuilder.reflectionToString(userDto));
}
print(userDtos.iterator(), "after changing return list... ");
} private static void print(Iterator iterator, String note) {
System.out.println(note);
while (iterator.hasNext()) {
System.out.println(ToStringBuilder.reflectionToString(iterator.next()));
}
} static class UserEntity {
private int id;
private String name; UserEntity(int id, String name) {
this.id = id;
this.name = name;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
}
} static class UserDto {
private int id;
private String name; UserDto(){} UserDto(int id, String name) {
this.id = id;
this.name = name;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
}
}
}

  

输出:
before change return list...
org.ryan.other.ListReference$UserDto@5debf305[id=1,name=x]
org.ryan.other.ListReference$UserDto@52a9fd96[id=2,name=y]
changing return list...
org.ryan.other.ListReference$UserDto@1647ad40[id=101,name=x]
org.ryan.other.ListReference$UserDto@3bbf502d[id=102,name=y]
after changing return list...
org.ryan.other.ListReference$UserDto@d28d900[id=1,name=x]
org.ryan.other.ListReference$UserDto@74be95bf[id=2,name=y]
after change return list...
org.ryan.other.ListReference$UserDto@c596a7a[id=1,name=x]
org.ryan.other.ListReference$UserDto@425d75eb[id=2,name=y]
after change from list...
org.ryan.other.ListReference$UserDto@5e8b957[id=1,name=x_change]
org.ryan.other.ListReference$UserDto@71e001c8[id=2,name=y_change]

  

  

com.google.common.collect.Lists#transform使用注意的更多相关文章

  1. java.lang.NoSuchMethodError: com.google.common.collect.Maps.newConcurrentMap()Ljava/util/concurrent/Concurren‌​tMap;

    在storm启动topo的时候,报错: java.lang.NoSuchMethodError: com.google.common.collect.Maps.newConcurrentMap()Lj ...

  2. Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/collect/Maps

    加入jar包 http://jarfiles.pandaidea.com/google.collect.html google-collect-1.0.jar.zip ( 504.8 KB )

  3. Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap

    selenium + java + mac + idea 报错分析: 网上搜的教程,配置selenium 自动化测试环境,都是只让导入 client-combined-3.141.59-sources ...

  4. guava Lists.transform使用

    作用:将一个List中的实体类转化为另一个List中的实体类. 稍微方便一点.例如:将List<Student>转化为List<StudentVo> Student: pack ...

  5. Guava Lists.transform踩坑小记<转>

    1.问题提出 1.前段时间在项目中用到Lists.transform返回的List,在对该list修改后发现修改并没有反映在结果里,研究源码后发现问题还挺大.下面通过单步调试的结果来查看Guava L ...

  6. Transformer-view java实体 转换视图 Lists.transform

    自: https://blog.csdn.net/mnmlist/article/details/53870520 meta_ws 连接: https://github.com/kse-music/d ...

  7. Idea运行时Scala报错Exception in thread "main" java.lang.NoSuchMethodError:com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;Ljava/lang/Object;)V

    一.情况描述 使用idea +scala+spark,运行程序代码如下: package cn.idcast.hello import org.apache.spark.rdd.RDD import ...

  8. 出现java.lang.NoClassDefFoundError: com/google/common/base/Charsets异常错误

    使用selenium,出现java.lang.NoClassDefFoundError: com/google/common/base/Charsets异常错误 原因:selenium-server- ...

  9. com.google.common.eventbus.EventBus介绍

    以下内容直接翻译了EventBus的注释: com.google.common.eventbus.EventBus介绍: 首先这个类是线程安全的, 分发事件到监听器,并提供相应的方式让监听器注册它们自 ...

随机推荐

  1. Kemaswill 机器学习 数据挖掘 推荐系统 Ranking SVM 简介

    Ranking SVM 简介 排序一直是信息检索的核心问题之一,Learning to Rank(简称LTR)用机器学习的思想来解决排序问题(关于Learning to Rank的简介请见我的博文Le ...

  2. JS放大镜特效(兼容版)

    原理 1.鼠标在小图片上移动时,通过捕获鼠标在小图片上的位置,定位大图片的相应位置 设计 1.页面元素:小图片.大图片.放大镜 2.技术点:事件捕获.定位 1)onmouseover:会在鼠标指针移动 ...

  3. Python中关于XML-RPC原理

    SimpleXMLRPCServer模块为XML-RPC服务端的写入提供了一个基本的框架.利用SimpleXMLRPCServer服务器既可以一直空闲,也可以利用CGIXMLRPCRequestHan ...

  4. 给Activity切换加入动画

    在startActivity或finish()后,调用overridePendingTransition方法,可以加入动画效果.例如: 使用Android自带的淡入淡出:android.R.anim. ...

  5. iOS-王云鹤 APP首次启动显示用户指导

    这个功能的重点就是在如何判断应用是第一次启动的. 其实很简单 我们只需要在一个类里面写好用户引导页面  基本上都是使用UIScrollView 来实现, 新建一个继承于UIViewController ...

  6. 用curl自动登录HTTPS站点

    前文http://blog.csdn.net/sheismylife/article/details/9237925 演示了如何手动的通过运行curl命令登录HTTPS站点,然后获取cookie, 再 ...

  7. VS2012下基于Glut 矩阵变换示例程序2:

    在VS2012下基于Glut 矩阵变换示例程序:中我们在绘制甜圈或者圆柱时使用矩阵对相应的坐标进行变换后自己绘制甜圈或者圆柱.我们也可以使用glLoadMatrixf.glLoadMatrixd载入变 ...

  8. <我的外骨骼,诺基>后的访谈

    <我的外骨骼,诺基>后的访谈 在接下采访任务之前,我对杨贵福的了解仅仅限于:有名的科幻作家,男性,成年.我虽然读过他的许多作品,但在这些作品中并不能找到作者的影子.我试着从作品的行文风格中 ...

  9. [转载]关于shell脚本的基本语法

    关于shell脚本的基本语法 整理于:2014-03-31,何俭飞,mymladdr@sina.com 一.执行 1.shell脚本如果要被执行,一般地必须要有执行权限"x"(除了 ...

  10. URL传值问题,不同浏览器对URL的长度要求

    通过URL传值的问题,所以对url字符串进行encodeURIComponent对url字符串内容进行编码,问题解决,但是有时候会出现 The request filtering module is ...