Java8 lambda遍历集合及BeanUtils.copyProperties()方法小试

package com.pfq.deal.audit.biz.server;

import java.util.ArrayList;

import java.util.List;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.BeanUtils;

/**

 * 

 * @author Dabria_ly

 * 2017年7月31日

 */

public class TestCopy {

private static final Logger LOG = LoggerFactory.getLogger(TestCopy.class);

public static void main(String[] args) {

List<TestCopyEntity> tceList = new ArrayList<TestCopyEntity>();

TestCopyEntity tce1 = new TestCopyEntity(1, “张三”, “信息描述1”);

TestCopyEntity tce2 = new TestCopyEntity(2, “李四”, “信息描述2”);

TestCopyEntity tce3 = new TestCopyEntity(3, “王五”, “信息描述3”);

tceList.add(tce1);

tceList.add(tce2);

tceList.add(tce3);

List<TestCopyVO1> tcv1List = new ArrayList<TestCopyVO1>();//TestCopyVO1和TestCopyEntity对象的字段相同:id,name,message

List<TestCopyVO2> tcv2List = new ArrayList<TestCopyVO2>();//TestCopyVO2比TestCopyEntity对象的字段少一个message

List<TestCopyVO3> tcv3List = new ArrayList<TestCopyVO3>();////TestCopyVO3比TestCopyEntity对象的字段多一个infoDir

//打印赋值之前的值

System.out.println(“赋值之前===========>”);

tcv1List.stream().forEach(System.out::println);

tcv2List.stream().forEach(System.out::println);

tcv3List.stream().forEach(System.out::println);

//循环赋值

tceList.stream().forEach(tce->{

TestCopyVO1 tcv1 = new TestCopyVO1();

TestCopyVO2 tcv2 = new TestCopyVO2();

TestCopyVO3 tcv3 = new TestCopyVO3();

BeanUtils.copyProperties(tce, tcv1);//将tce赋值给tcv1

BeanUtils.copyProperties(tce, tcv2);//将tce赋值给tcv2

BeanUtils.copyProperties(tce, tcv3);//将tce赋值给tcv3

//添加到list

tcv1List.add(tcv1);

tcv2List.add(tcv2);

tcv3List.add(tcv3);

});

//打印赋值之后的值

System.out.println(“赋值之后========>”);

tcv1List.stream().forEach(System.out::println);

tcv2List.stream().forEach(System.out::println);

tcv3List.stream().forEach(System.out::println);

//java8自带时间格式化类 System.out.println(LocalDate.now().format(DateTimeFormatter.ISO_DATE));

}

}

//——————————————————————————————————————–

package com.pfq.deal.audit.biz.server;

/**
 * 
 * @author Dabria_ly
 * 2017年7月31日
 */
public class TestCopyEntity {
private Integer id;
private String name;
private String message;

public TestCopyEntity() {}

public TestCopyEntity(Integer id, String name, String message) {
this.id = id;
this.name = name;
this.message = message;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

}

//——————————————————————————–

package com.pfq.deal.audit.biz.server;
/**
 * 
 * @author Dabria_ly
 * 2017年7月31日
 */
public class TestCopyVO1 {
private Integer id;
private String name;
private String message;

@Override
public String toString() {
return “TestCopyVO1 [id=” + id + “, name=” + name + “, message=” + message + “]”;
}

public TestCopyVO1() {}

public TestCopyVO1(Integer id, String name, String message) {
this.id = id;
this.name = name;
this.message = message;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
//—————————————————————————-

package com.pfq.deal.audit.biz.server;
/**
 * 
 * @author Dabria_ly
 * 2017年7月31日
 */
public class TestCopyVO2 {
private Integer id;
private String name;

@Override
public String toString() {
return “TestCopyVO2 [id=” + id + “, name=” + name + “, message=” + “]”;
}

public TestCopyVO2() {}

public TestCopyVO2(Integer id, String name) {
this.id = id;
this.name = name;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

//———————————————————————-

package com.pfq.deal.audit.biz.server;

/**
 * 
 * @author Dabria_ly
 *  2017年7月31日
 */
public class TestCopyVO3 {
private Integer id;
private String name;
private String message;
private String infoDir;

@Override
public String toString() {
return “TestCopyVO3 [id=” + id + “, name=” + name + “, message=” + message + “, infoDir=” + infoDir + “]”;
}

public TestCopyVO3() {}

public TestCopyVO3(Integer id, String name, String message, String infoDir) {
this.id = id;
this.name = name;
this.message = message;
this.infoDir = infoDir;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String getInfoDir() {
return infoDir;
}

public void setInfoDir(String infoDir) {
this.infoDir = infoDir;
}

}

    原文作者:Dabria_ly
    原文地址: https://blog.csdn.net/Dabria_ly/article/details/76687908
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞