作用:将一个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使用的更多相关文章

  1. Guava Lists.transform踩坑小记<转>

    1.问题提出 1.前段时间在项目中用到Lists.transform返回的List,在对该list修改后发现修改并没有反映在结果里,研究源码后发现问题还挺大.下面通过单步调试的结果来查看Guava L ...

  2. com.google.common.collect.Lists#transform使用注意

    /** * Returns a list that applies {@code function} to each element of {@code * fromList}. The return ...

  3. Transformer-view java实体 转换视图 Lists.transform

    自: https://blog.csdn.net/mnmlist/article/details/53870520 meta_ws 连接: https://github.com/kse-music/d ...

  4. Lists.transform的使用

    转自:https://blog.csdn.net/weixin_42201566/article/details/81513769 Lists.transform:能够轻松的从一种类型的list转换为 ...

  5. 在使用Lists.transform时,不会直接生成PurchaseOrderVo的集合对象,而是生成一个Function的集合

    但是在使用Lists.transform时,不会直接生成PurchaseOrderVo的集合对象,而是生成一个Function的集合,在循环的时候,会去调用apply 生成一个PurchaseOrde ...

  6. [置顶] Guava学习之Lists

    Lists类主要提供了对List类的子类构造以及操作的静态方法.在Lists类中支持构造ArrayList.LinkedList以及newCopyOnWriteArrayList对象的方法.其中提供了 ...

  7. Guava包学习---Lists

    Guava包是我最近项目中同事推荐使用的,是google推出的库.里面的功能非常多,包括了集合.缓存.原生类型支持.并发库.通用注解.字符串处理.IO等.我们项目中使用到了guava依赖,但是实际上只 ...

  8. guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用

    guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用 1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection ...

  9. guava函数式编程

    [Google Guava] 4-函数式编程 原文链接 译文链接 译者:沈义扬,校对:丁一 注意事项 截至JDK7,Java中也只能通过笨拙冗长的匿名类来达到近似函数式编程的效果.预计JDK8中会有所 ...

随机推荐

  1. 关于jQuery实现CheckBox全选只能生效一次的问题

    //这代码只有一次全选.全不选的效果 第三次点击checkall会没有任何效果 $("#checkall").click(function(){ $('input[name=&qu ...

  2. Django REST framewor

      4.1 RESTfull 4.1.1.前后端不分离 前端看到什么效果,由后端决定,前端页面看到的效果是有后端控制的 前后端耦合度很高 比较适合纯网页设计,如果有APP,还需要开发APP后端借口 4 ...

  3. C# receive 接收的字符串 对比不相等的问题

    背景: 最近写了一个C/S架构的 C# Socket 通讯项目,服务端,客户端弄通了之后,客户端发送给服务端后,服务端回复一个字符串告诉客户端确认收到了.问题就在这. 问题: 一开始,我让服务端回复了 ...

  4. Go Lang

    IDE: https://www.jetbrains.com/products.html?fromMenu#type=ide Study: http://www.runoob.com/go/go-en ...

  5. ansible 自动化运维

    Ansible 自动化运维 ansible安装epel #yum list all *ansible*#yum install *ansible*#yum info ansible#rpm -ql a ...

  6. 03MYSQL数据库

    mySQL 数据库 储存数据,属于中小型数据库   默认端口号 3306  密码root sql是一门编程语言 结构化查询语言  是强类型语言(定义变量时要指定变量类型) 字符串有两种类型:  定长: ...

  7. React项目中跨域问题的解决方案

    刚刚找到到通过creat-react-app创建的项目中跨域问题的解决方案,记录下来以备后用. 如果接口地址为:    http://my.example.com/eg-api  则配置package ...

  8. cin.ignore

    功能:函数用于输入流.它读入字符,直到已经读了num 个字符(默认为1)或是直到字符delim 被读入(默认为EOF).其调用形式为cin.ignore(n,终止字符)       原型:istrea ...

  9. asp.net 按钮执行前后台方法——前台弹出提示信息,确认后继续执行后台方法,取消则不执行后台方法

    我们做一个测试的web页面,只需要一个button+一个label就ok啦,通过button按钮的后台事件修改label的text属性值来测试是否执行了后台事件里的代码 前台 写一个js方法: < ...

  10. 使用DHCP动态管理主机地址