作用:将一个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. 实现在WebView中返回上一级

    代码 import React, {Component} from 'react'; import {Platform, View, WebView, BackHandler,Dimensions,S ...

  2. python中用psutil模块,yagmail模块监控CPU、硬盘、内存使用,阈值后发送邮件

    import yagmailimport psutildef sendmail(subject,contents): #连接邮箱服务器 yag = yagmail.SMTP(user='邮箱名称@16 ...

  3. 事务的ACID特性(转)

    ACID特性 数据库中的事务(Transaction)有四个特性,分别是:原子性(Atomicity),一致性(Consistency),隔离性(lsolation),持久性(Durability) ...

  4. Spring定义事物通知tx:advice

    <aop:config proxy-target-class="false">    <aop:advisor advice-ref="txAdvice ...

  5. shell中的函数、数组、报警系统脚本

    1.shell中的函数 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这 个小单元的名字即可.格式: function f_name() {commond} ...

  6. hello.java分析

    如下图源码所示: 该段代码声明了一个entity实体类,该类有一个变量name,对该变量写了对应的get和set方法.类中还有一个空的构造方法hello(). @RequestScoped用于指定一个 ...

  7. IntelliJ IDEA 使用教程

    在IDEA当中,Project和  Module是作为两个不同的概念,对项目结构是重要意义的 eclipse和idea的区别 IDEA里面的子工程要称为Module就是模块化的概念,作为聚合工程亦或普 ...

  8. ROS * 了解xacro的编写

    在urdf文件中,会有很多内容是一样的,当要改变这些语句重某一个相同的参数时却要更改很多次,很吗发,于是有了一种精简化.可复用.模块化的描述形式——xacro 废话少说 声明重复使用的常量 <? ...

  9. 中间件和Django缓存

    中间件定义: 中间件是一个.一个的管道,如果相对任何所有的通过Django的请求进行管理都需要自定义中间件 中间件可以对进来的请求和出去的请求进行控制 中间件是一类. 看下面的代码在settings里 ...

  10. 全志A33开发板的安卓控制LED-2-JNI基础

    虽然您可以完全使用Java编写应用程序,但有些情况下Java本身并不能满足您的应用程序的需求.当应用程序不能完全用Java编写时,程序员使用JNI编写Java本机方法来处理这些情况. 以下示例说明何时 ...