/**
* 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. Thrift RPC实战(一).初次体验Thrift

    1.前言: Thrift作为Facebook开源的RPC框架, 通过IDL中间语言, 并借助代码生成引擎生成各种主流语言的rpc框架服务端/客户端代码,主要特点: 开发速度快: 通过编写RPC接口ID ...

  2. python logging info -> 将服务请求记录输出

    在tornado 里面这样用 看看logging.warning() , logging.info() , 我们非常想用 zdaemon , 和 logging 将对系统的所有访问转换到服务器里面,作 ...

  3. linuxmint 15/ ubuntu 13.04 install OpenERP client 6.0.4

    As we all know OpenERP is a great open-source ERP/CRM project. It does help people a lot when workin ...

  4. Nginx之旅系列 - Nginx的configuration

    题记:Nginx之旅系列是用来记录Nginx从使用到源码学习的点点滴滴,分享学习Nginx的快乐 Nginx 首页: http://nginx.org/ Nginx的configuration 今天对 ...

  5. JMX操作ActiveMQ(2)

    默认情况下,ActiveMQ使用useJmx后,jmx的url为 service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi 这时,jmx的MBean se ...

  6. java中outer的使用

    outer多用于嵌套循环的情况 outer: for (int i = 2 ; i <= 10 ; i++) { for (int j = 2 ; j <=10 ; j++) { if(i ...

  7. 用C++实现的元胞自动机

    我是一个C++初学者,控制台实现了一个元胞自动机. 代码如下: //"生命游戏"V1.0 //李国良于2017年1月1日编写完成 #include <iostream> ...

  8. H5与CS3权威下.18 and 19 选择器(1)

    18章.CSS3概述 1.从前端技术的角度把互联网的发展分为三个阶段: (1)web1.0:HTML和CSS. (2)web2.0:Ajax,Javascript/DOM/异步数据请求. (3)web ...

  9. MongoDB应用案例:使用 MongoDB 存储日志数据

    线上运行的服务会产生大量的运行及访问日志,日志里会包含一些错误.警告.及用户行为等信息,通常服务会以文本的形式记录日志信息,这样可读性强,方便于日常定位问题,但当产生大量的日志之后,要想从大量日志里挖 ...

  10. 【C#】Deep copy of objects

    If you learned C++ carefully, you must have known something about the copy of object. For example, I ...