/**
* 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. 一位IT牛人的十年经验之谈

    1.分享第一条经验:“学历代表过去.能力代表现在.学习力代表未来.” 其实这是一个来自国外教育领域的一个研究结果.相信工作过几年.十几年的朋友对这个道理有些体会吧.但我相信这一点也很重要:“重要的道理 ...

  2. 我的Emacs折腾经验谈(一) 一些给新人的建议

    这几天都没有动力写mongodb的东西,我果然还是太懒了么~ 主要是没有一个系统的东西整理出来,加上我令人拙计的语言表达能力,这个坑只能慢慢再补了. 最近在折腾emacs这个东西,首先说我曾经算是个极 ...

  3. 巧用final

    1.final可以修饰函数的参数,以防止函数内部随意篡改不允许修改的参数. 2.在函数内部,把函数的局部变量声明为final类型,可以检查在函数内部它们是否的确只被赋值一次.

  4. java模式之装饰模式

    1. 什么叫装饰模式? 根据业务的需求,需要对一个类的方法进行增强的处理. 2. 为什么需要装饰模式? 拓展性更加的好,当觉得这个装饰不好的时候,可以直接拿下,不需要改变任何的代码. 3. 装饰模式的 ...

  5. 从零开始学C++之构造函数与析构函数(一):构造函数、析构函数、赋值与初始化、explicit关键字

    一.构造函数.默认构造函数 (1).构造函数 构造函数是特殊的成员函数 创建类类型的新对象,系统自动会调用构造函数 构造函数是为了保证对象的每个数据成员都被正确初始化 函数名和类名完全相同 不能定义构 ...

  6. mybatis中updateByPrimaryKeySelective

    mybatis中updateByPrimaryKeySelective等选择性操作在判断时对于VARCHAR类型需要同时判断非空和非空串 <if test="description ! ...

  7. 《linux内核完全剖析》笔记03-进程创建

    根据一下问题来看笔记 进程占多大的线形地址空间 进程实际分配多少物理内存 创建进程的开销在哪里 一. 从fork系统调用开始 kernel/sys_call.s第222行 _sys_fork: cal ...

  8. MP4视频编码器

    用 HTML5 播放视频,最流行的视频格式非 MP4莫属, 所有最新浏览器都支持; MP4 带有 H.264 视频编码和 AAC 音频编码的 MPEG 4 文件: 以前做过一个把其它编码格式的视频转成 ...

  9. Swift Array copy 的线程安全问题

    Swift Array copy 的线程安全问题 NSArray 继承自 NSObject,属于对象,有 copy 方法.Swift 的 Array 是 struct,没有 copy 方法.把一个 A ...

  10. Docker集群实验环境布署--swarm【1 架构说明】

    在读完<Docker技术入门与实践>这本书后,基本上已对Docker了有一些入门的理解,以及我们为什么要使用Docker 答:我们发现在实际工作中,通过openstack一旦把一个VM创建 ...