/**
* 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. zabbix实现对磁盘动态监控

    zabbix实现对磁盘动态监控 前言 zabbix一直是小规模互联网公司服务器性能监控首选,首先是免费,其次,有专门的公司和社区开发维护,使其稳定性和功能都在不断地增强和完善.zabbix拥有详细的U ...

  2. 为什么不能在子类或外部发布C#事件

    为什么不能在子类或外部发布C#事件 背景 一个朋友问了一个问题:“为什么不能在子类或外部发布C#事件?”,我说我不知道,要看看生产的IL代码,下面我们看看. 测试 代码 1 using System; ...

  3. hdu 2859 (二维dp)

    点击打开链接 题意: 给你一个n*n的矩阵,矩阵中只含有26个小写字母,求其中最大的对称矩阵的大小 当我们算到s[i][j]时,每次我们只需要将它上方的和右方的依次比较,看是否相同 注意这里不能只比较 ...

  4. MFC控件(9):network address control

    这个控件的名字倒是取的不错,一看就知道是让你输入IP地址或host name的. 不过一打开看到那控件的样子就完全是个Edit control.不过该控件对应的类也确实是继承自类CEdit. 先拖个控 ...

  5. 使用Bundle在Activity之间交换数据

    一:在main.xml文件中设置布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...

  6. Nginx学习之十四-GDB调试Nginx初试

    本文的测试环境: Win7+虚拟机VMWareVMware-workstation-full-7.1.4-385536+Ubuntu12.04 Nginx-1.4.0 要想有效的研究Nginx源码,必 ...

  7. Android实现通过手机找回密码

    其实这篇文章有点标题党,代码没有几行,真不好意思,但是确实给出了解决方案,这个我觉得还是很重要的,代码写了几年之后其实比较注重的就是思路跟方法了. 背景:手机客户端有这么一个需求,用户忘记密码了,然后 ...

  8. 理解git分支-远程分支

    远程分支 远程引用是对远程仓库的引用(指针),包括分支.标签等等. 你可以通过 git ls-remote (remote)来显式地获得远程引用的完整列表,或者通过 git remote show ( ...

  9. JSP引擎的工作原理

    JSP运行环境: 执行JSP代码需要在服务器上安装JSP引擎,比较常见的引擎有WebLogic和Tomcat.把这些支持JSP的web服务器配置好后.就可以再客户端通过浏览器来访问JSP页面了.默认端 ...

  10. 用PopWindow做下拉框

    最近在做下拉框,本来想用spinner,可是spinner达不到项目要求,跟同学同事问了一圈,都在用popwindow, 网上看了一下,popwindow挺简单的,可定制性挺强的,符合我的要求,所以, ...