com.google.common.collect.Lists#transform使用注意
/**
* 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使用注意的更多相关文章
- java.lang.NoSuchMethodError: com.google.common.collect.Maps.newConcurrentMap()Ljava/util/concurrent/ConcurrentMap;
在storm启动topo的时候,报错: java.lang.NoSuchMethodError: com.google.common.collect.Maps.newConcurrentMap()Lj ...
- 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 )
- Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap
selenium + java + mac + idea 报错分析: 网上搜的教程,配置selenium 自动化测试环境,都是只让导入 client-combined-3.141.59-sources ...
- guava Lists.transform使用
作用:将一个List中的实体类转化为另一个List中的实体类. 稍微方便一点.例如:将List<Student>转化为List<StudentVo> Student: pack ...
- Guava Lists.transform踩坑小记<转>
1.问题提出 1.前段时间在项目中用到Lists.transform返回的List,在对该list修改后发现修改并没有反映在结果里,研究源码后发现问题还挺大.下面通过单步调试的结果来查看Guava L ...
- Transformer-view java实体 转换视图 Lists.transform
自: https://blog.csdn.net/mnmlist/article/details/53870520 meta_ws 连接: https://github.com/kse-music/d ...
- 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 ...
- 出现java.lang.NoClassDefFoundError: com/google/common/base/Charsets异常错误
使用selenium,出现java.lang.NoClassDefFoundError: com/google/common/base/Charsets异常错误 原因:selenium-server- ...
- com.google.common.eventbus.EventBus介绍
以下内容直接翻译了EventBus的注释: com.google.common.eventbus.EventBus介绍: 首先这个类是线程安全的, 分发事件到监听器,并提供相应的方式让监听器注册它们自 ...
随机推荐
- SubSonic3.0配置及使用
SubSonic3.0配置及使用 SubSonic is A Super High-fidelity Batman Utility Belt that works up your Data Acces ...
- Android开发中与服务器交互时,遇到java.io.IOException: Target host must not be null的问题
当我遇到这个问题的时候,也在网上查找好半天.找到了一个和这个问题很类似的问题——java.lang.IllegalStateException: Target host must not be nul ...
- kivy create a package for Android
Now that you've successfully coded an app. Now you want to deploy it to Android. So now we would nee ...
- oracle 11g使用deferred_segment_creation 延迟段创建特性时遇到的问题总结
总结,下面是两个问题.问题1是用户可以在所有表空间创建表;问题2是exp不能导出空表 问题1: 版本:oracle 11.2.0.1.0 select * from v$version; 创建用户aa ...
- cpu组相连映射
组相联映射方式 组相联映射方式 是全相联映射方式和直接相联映射方式的结合,结合两者的优点 · 方法:把Cache分为若干组,每组含有若干行. 组间直接映射,组内全相联映射. (图7) · 映射过程:( ...
- spring请求到达controller但响应404
问题是这样的,前台发送请求的后台,后台的方法正常执行,将数据放在response.getWrite里,但在前台并没有展示数据.用浏览器的开发者工具看下请求,发现响应404. 最后网上查了查,sprin ...
- Android之WebView网页滚动截图
WebView 网页滚动截屏,可对整个网页进行截屏而不是仅当前屏幕哦! 注意若Web页面存在position:fixed; 的话得在调用前设置为 position:absolute; 哦,否则会出现很 ...
- nodejs,http,get,post,请求
本文源于实践及其部分网络搜索: 其实大部分,官方都有介绍... 官方参考链接:https://nodejs.org/api/http.html var http = require('http'); ...
- Threejs基础学习【修改版】
一. Three.js官网及使用Three.js必备的三个条件 1.Three.js 官网 https://threejs.org/ 2.使用Three.js必备的三个条件(To actually b ...
- jmeter连接数据库
新建一个 Thread Group: 新增 JDBC Connection Configuration: 点击新增的 JDBC Connection Configuration ,需要修改的参数包括: ...