作用:将一个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. html 使表格随着内容自动适应宽度

    所谓难而不会,会儿不难.这个问题让我纠结了很长时间,一句css解决了,仅仅靠一个属性 td { white-space: nowrap; } from:http://blog.csdn.net/liu ...

  2. 入门项目 A6 lib common 登陆状态装饰

    # 用户认证装饰器 def auth(func): from core import src def inner(*args, **kwargs): # 调用被装饰函数前需要做的操作 if src.u ...

  3. 2018-计算机系机试-A

    #include<stdio.h> #include<cstdio> #include<cmath> #include<cstring> #includ ...

  4. 部署Qt应用时候报错0xc000007b

    情况: 在开发环境可以运行,部署到其他电脑无法运行: 排错:百度.谷歌了很多方法不行,后来发现添加了Qt\5.11.0\mingw53_32\bin环境变量,程序执行正常,去掉就报错:猜测估计是dll ...

  5. Delphi7连接MySql数据库-DBGrid控件显示数据

    一个简单的Delphi7小程序,使用MySql数据库做简单查询,用DBGrid控件显示结果,实现过程如下: (1)在MySql中新建demouser表,插入记录用于测试. (2)在Delphi7中新建 ...

  6. ES6学习重难点总结(持续更新)

    Symbol 1.Symbol.iterator Symbol.iterator指向对象的默认遍历器方法: String.prototype[Symbol.iterator] //ƒ [Symbol. ...

  7. pagerank算法在数学模型中的运用(有向无环图中节点排序)

    一.模型介绍 pagerank算法主要是根据网页中被链接数用来给网页进行重要性排名. 1.1模型解释 模型核心: a. 如果多个网页指向某个网页A,则网页A的排名较高. b. 如果排名高A的网页指向某 ...

  8. 测试那些事儿—软测必备的Linux知识(四)

    1.文件权限管理 ls -l 显示的内容如下: 10个字符确定不同用户能对文件干什么 第一个字符:-表示文件,d表示目录,l表示链接 其余字符每3个一组(rwx),r-读,w-写,x-执行 第一组rw ...

  9. complex类的设计实现

    #include <iostream> #include <cmath> using namespace std; class Complex{ ,); Complex(Com ...

  10. CentOS官网下载系统镜像

    https://jingyan.baidu.com/article/1876c85279cedd890a13766c.html