1. 程式人生 > >Java8 lambda遍歷集合及BeanUtils.copyProperties()方法小試

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;
}


}