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中会有所 ...
随机推荐
- 转换流 InputStreamReader
通常接触到字节流和字符流,但是有一个流是这两个流的桥梁,inputStreamReader 字符流的结构如下 可以看到inputStreamReader是继承Reader ,它的子类是FileRead ...
- xcfe桌面快捷键整理
转载自:https://my.oschina.net/u/565351/blog/502018 commands custom <Alt>F1:xfce4-popup-applicatio ...
- SQL注入之Sqli-labs系列第四十七关,第四十八关,第四十九关(ORDER BY注入)
0x1 源码区别点 将id变为字符型:$sql = "SELECT * FROM users ORDER BY '$id'"; 0x2实例测试 (1)and rand相结合的方式 ...
- the evolution of Lua 全文翻译
终于赶在春节前将论文全文翻译完,以后有时间将前面三章重新翻译一次,因为刚开始的时候没打算全文翻译的..第一次每天花25分钟完成这么长的一篇翻译,证明滴水可以穿石,哈哈哈 中文地址:Lua的演进 祝各位 ...
- 【集合】Java中的具体集合(一)
Java中不止提供了集合框架中的接口,还提供了许多具体的实现. Java中的具体集合 集合类型 描述 ArrayList 一种可以动态增长和缩减的索引序列 LinkedList 一种可以在任何位置进行 ...
- java GMT时间转换为CST时间
今天在获取windchill的“上次修改时间”的时候发现获取的是GMT时间,但是我们常用的是CST时间 一开始尝试直接设置为CST时区,TimeZone.getTimeZone("CST&q ...
- python中shutil模块的使用
可以操作权限的处理文件模块:shutil # 基于路径的文件复制 import shutil shutil.copyfile("oldfile_path","newfil ...
- 根据某字段将其他字段进行拼接的两种方法(SYS_CONNECT_BY_PATH及wm_concat)
秘书姐姐说想知道她发起的所有流程,现在都到谁审批了.由于一条流程当前审批人可能有多个,故需根据单据编号(djbh)将审批人拼接到一个字段中. 说明: wfn审批历史记录表,djbh 单据编号,pk_c ...
- Map<String,Integer>acount字符串出现的次数
- kylin cubing algorithm(算法)
看到这一块的视频,结合光方博客的一些文档及自己的一点理解,记个笔记,以备不时之需. by layer cubing 1.on MR 这个算法的对cube的计算就像它的名字一样是按player进行的. ...