guava Lists.transform使用
作用:将一个List中的实体类转化为另一个List中的实体类。
稍微方便一点。例如:将List<Student>转化为List<StudentVo>
Student:
package com.cy.model; import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle; public class Student{
private String id;
private String name;
private String stuNo;
private String address;
private String good; public Student(){ } public Student(String id, String name, String stuNo, String address, String good) {
this.id = id;
this.name = name;
this.stuNo = stuNo;
this.address = address;
this.good = good;
} public String getGood() {
return good;
} public void setGood(String good) {
this.good = good;
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getStuNo() {
return stuNo;
} public void setStuNo(String stuNo) {
this.stuNo = stuNo;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} @Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
StudentVo:
package com.cy.vo; import com.cy.model.Student;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle; public class StudentVo {
private String student_id;
private String name;
private String student_no; public StudentVo(){ } public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getStudent_id() {
return student_id;
} public void setStudent_id(String student_id) {
this.student_id = student_id;
} public String getStudent_no() {
return student_no;
} public void setStudent_no(String student_no) {
this.student_no = student_no;
} @Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
package com.cy.test.guava; import com.cy.model.Student;
import com.cy.vo.StudentVo;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import org.apache.commons.beanutils.BeanUtils;
import java.util.ArrayList;
import java.util.List; public class GuavaTest { public static void main(String[] args) {
//guava Lists.transform测试 List<Student> studentList = new ArrayList<>();
studentList.add(new Student("1","zhangsan","no-001", "zhaotan", "true"));
studentList.add(new Student("2","lisi","no-002", "qingshan", "true"));
studentList.add(new Student("3","wangwu","no-003", "guangang", "false"));
System.out.println(studentList); List<StudentVo> studentVoList = Lists.transform(studentList, new Function<Student, StudentVo>() {
@Override
public StudentVo apply(Student student) {
StudentVo s = new StudentVo();
try {
BeanUtils.copyProperties(s, student);
} catch (Exception e) {
}
s.setStudent_id(student.getId());
s.setStudent_no(student.getStuNo());
return s;
}
});
System.out.println(studentVoList);
}
}
打印:
[Student[id=1,name=zhangsan,stuNo=no-001,address=zhaotan,good=true], Student[id=2,name=lisi,stuNo=no-002,address=qingshan,good=true], Student[id=3,name=wangwu,stuNo=no-003,address=guangang,good=false]]
[StudentVo[student_id=1,name=zhangsan,student_no=no-001], StudentVo[student_id=2,name=lisi,student_no=no-002], StudentVo[student_id=3,name=wangwu,student_no=no-003]]
依赖:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
guava Lists.transform使用的更多相关文章
- Guava Lists.transform踩坑小记<转>
1.问题提出 1.前段时间在项目中用到Lists.transform返回的List,在对该list修改后发现修改并没有反映在结果里,研究源码后发现问题还挺大.下面通过单步调试的结果来查看Guava L ...
- com.google.common.collect.Lists#transform使用注意
/** * Returns a list that applies {@code function} to each element of {@code * fromList}. The return ...
- Transformer-view java实体 转换视图 Lists.transform
自: https://blog.csdn.net/mnmlist/article/details/53870520 meta_ws 连接: https://github.com/kse-music/d ...
- Lists.transform的使用
转自:https://blog.csdn.net/weixin_42201566/article/details/81513769 Lists.transform:能够轻松的从一种类型的list转换为 ...
- 在使用Lists.transform时,不会直接生成PurchaseOrderVo的集合对象,而是生成一个Function的集合
但是在使用Lists.transform时,不会直接生成PurchaseOrderVo的集合对象,而是生成一个Function的集合,在循环的时候,会去调用apply 生成一个PurchaseOrde ...
- [置顶] Guava学习之Lists
Lists类主要提供了对List类的子类构造以及操作的静态方法.在Lists类中支持构造ArrayList.LinkedList以及newCopyOnWriteArrayList对象的方法.其中提供了 ...
- Guava包学习---Lists
Guava包是我最近项目中同事推荐使用的,是google推出的库.里面的功能非常多,包括了集合.缓存.原生类型支持.并发库.通用注解.字符串处理.IO等.我们项目中使用到了guava依赖,但是实际上只 ...
- guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用
guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用 1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection ...
- guava函数式编程
[Google Guava] 4-函数式编程 原文链接 译文链接 译者:沈义扬,校对:丁一 注意事项 截至JDK7,Java中也只能通过笨拙冗长的匿名类来达到近似函数式编程的效果.预计JDK8中会有所 ...
随机推荐
- 判断库中为字符串格式的时间是否为最近三个月(Java)
今天分享一个问题,就是标题中提到的问题,今天在调用一个接口的时候,发现调用到的数据的时间格式为字符串类型,我有点蒙圈,于是,我就百度解决了这个问题,同时在这里记录一下,为了之后不再蒙圈::: 首先需要 ...
- you-get 下载视频
亲测有效,没在别的平台试,道理是相通的 平台:Windows 10 所需工具: python3,pip3,you-get 步骤流程: 正确安装python3,配置环境变量 (目前使用的是3.6+) 打 ...
- 【Linux】scp指令
语法: scp [可选参数] file_source file_target 参数说明: -1: 强制scp命令使用协议ssh1 -2: 强制scp命令使用协议ssh2 -4: 强制scp命令只使用I ...
- angularjs - 自定义指令(directive)
自定义指令(directive) 使用 .directive 函数来添加自定义的指令. 要调用自定义指令,HTML 元素上需要添加自定义指令名. 例子:使用驼峰法来命名一个指令, demoDirect ...
- qt+opencv LNK4272:library machine type 'x64' conflicts with target mathine type 'x86'
运行时报错如上图所示,原因是你使用的opencv库是64位的,qt里面使用的编译器MSVC是32位的,解决方法如下: 将构建套件修改位64bit:
- 使用python编辑和读取word文档
python调用word接口主要用到的模板为python-docx,基本操作官方文档有说明. python-docx官方文档地址 使用python新建一个word文档,操作就像文档里介绍的那样: fr ...
- 游戏编程模式 Game Programming Patterns (Robert Nystrom 著)
第1篇 概述 第1章 架构,性能和游戏 (已看) 第2篇 再探设计模式 第2章 命令模式 (已看) 第3章 享元模式 (已看) 第4章 观察者模式 (已看) 第5章 原型模式 (已看) 第6章 单例模 ...
- 事件冒泡(event bubbling)与事件捕捉(event capturing)
事件捕捉: 单击<div>元素就会以下列顺序触发click 事件. Document => Element html => Element body => Element ...
- 深入理解CSS系列(二):为什么height:100%不生效?
对于height属性,如果父元素height为auto,只要子元素在文档流中(即position不等于fixed或者absolute),其百分比值完全就被忽略了.这是什么意思呢?首先来看个例子,比如, ...
- JIT(Just in time,即时编译,边运行边编译)、AOT(Ahead Of Time,运行前编译),是两种程序的编译方式
JIT(Just in time,即时编译,边运行边编译).AOT(Ahead Of Time,运行前编译),是两种程序的编译方式