需要从List<A> copy到List<B>中。其中A类和B类拥有相同的字段(或者是部分字段相同)
这是第一个A类----SysMessage类
public class SysMessage {
protected String id;
protected String subject;
protected String ownerId;
protected String owner;
protected String messageType;
protected Date createTime;
protected Short canReply;
protected Short isPublic;
protected String content;
protected String fileMsg;
protected String receiverName;
protected Date receiveTime;
protected String receiverId;
protected String receiverOrgName;
protected String receiverOrgId;
protected String rid;
public SysMessage() {
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return this.id;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getSubject() {
return this.subject;
}
public void setOwnerId(String ownerId) {
this.ownerId = ownerId;
}
public String getOwnerId() {
return this.ownerId;
}
public void setOwner(String owner) {
this.owner = owner;
}
public String getOwner() {
return this.owner;
}
public void setMessageType(String messageType) {
this.messageType = messageType;
}
public String getMessageType() {
return this.messageType;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getCreateTime() {
return this.createTime;
}
public void setCanReply(Short canReply) {
this.canReply = canReply;
}
public Short getCanReply() {
return this.canReply;
}
public void setIsPublic(Short isPublic) {
this.isPublic = isPublic;
}
public Short getIsPublic() {
return this.isPublic;
}
public void setContent(String content) {
this.content = content;
}
public String getContent() {
return this.content;
}
public void setFileMsg(String fileMsg) {
this.fileMsg = fileMsg;
}
public String getFileMsg() {
return this.fileMsg;
}
public Date getReceiveTime() {
return this.receiveTime;
}
public void setReceiveTime(Date receiveTime) {
this.receiveTime = receiveTime;
}
public String getReceiverName() {
return this.receiverName;
}
public void setReceiverName(String receiverName) {
this.receiverName = receiverName;
}
public String getReceiverOrgName() {
return this.receiverOrgName;
}
public void setReceiverOrgName(String receiverOrgName) {
this.receiverOrgName = receiverOrgName;
}
public String getReceiverId() {
return this.receiverId;
}
public void setReceiverId(String receiverId) {
this.receiverId = receiverId;
}
public String getReceiverOrgId() {
return this.receiverOrgId;
}
public void setReceiverOrgId(String receiverOrgId) {
this.receiverOrgId = receiverOrgId;
}
public String getRid() {
return this.rid;
}
public void setRid(String rid) {
this.rid = rid;
}
public String toString() {
return (new ToStringBuilder(this)).append("id", this.id).append("subject", this.subject).append("ownerId", this.ownerId).append("owner", this.owner).append("messageType", this.messageType).append("createTime", this.createTime).append("canReply", this.canReply).append("isPublic", this.isPublic).append("content", this.content).append("fileMsg", this.fileMsg).toString();
}
}
这是第二个B类------Message
public class Message {
protected String id;
protected String subject;
protected String ownerId;
protected String owner;
protected String messageType;
protected Date createTime;
protected Short canReply;
protected Short isPublic;
protected String content;
protected String fileMsg;
protected String receiverName;
protected Date receiveTime;
protected String receiverId;
protected String receiverOrgName;
protected String receiverOrgId;
protected String rid;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getOwnerId() {
return ownerId;
}
public void setOwnerId(String ownerId) {
this.ownerId = ownerId;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public String getMessageType() {
return messageType;
}
public void setMessageType(String messageType) {
this.messageType = messageType;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Short getCanReply() {
return canReply;
}
public void setCanReply(Short canReply) {
this.canReply = canReply;
}
public Short getIsPublic() {
return isPublic;
}
public void setIsPublic(Short isPublic) {
this.isPublic = isPublic;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getFileMsg() {
return fileMsg;
}
public void setFileMsg(String fileMsg) {
this.fileMsg = fileMsg;
}
public String getReceiverName() {
return receiverName;
}
public void setReceiverName(String receiverName) {
this.receiverName = receiverName;
}
public Date getReceiveTime() {
return receiveTime;
}
public void setReceiveTime(Date receiveTime) {
this.receiveTime = receiveTime;
}
public String getReceiverId() {
return receiverId;
}
public void setReceiverId(String receiverId) {
this.receiverId = receiverId;
}
public String getReceiverOrgName() {
return receiverOrgName;
}
public void setReceiverOrgName(String receiverOrgName) {
this.receiverOrgName = receiverOrgName;
}
public String getReceiverOrgId() {
return receiverOrgId;
}
public void setReceiverOrgId(String receiverOrgId) {
this.receiverOrgId = receiverOrgId;
}
public String getRid() {
return rid;
}
public void setRid(String rid) {
this.rid = rid;
}
}
现在有一个 List<SysMessage> messages。为我们需要把messages的数据复制到 List<Message> messageList中去。有两种方法。
1.使用JSON转化成字符串(string),再反序列化成list。
/**
* 从List<A> copy到List<B>
* @param list List<B>
* @param clazz B
* @return List<B>
*/
public <T> List<T> copy(List<?> list,Class<T> clazz){
String oldOb = JSON.toJSONString(list);
return JSON.parseArray(oldOb, clazz);
}
2.BeanUtils.copyProperties(Object dest, Object orig)方法
/**
* 从List<A> copy到List<B>
* @param list List<B>
* @param clazz B
* @param String s 占位参数
* @return List<B>
*/
public <T> List<T> copy(List<?> list,Class<T> clazz,String s) throws Exception{
List<T> tList = new ArrayList<>();
for(Object obj: list){
T t = clazz.newInstance();
BeanUtils.copyProperties(t,obj);
tList.add(t);
}
return tList;
}
相比于第一种方式,通过循环创建对象的方式适合于数据量较多时或者是请求的数量较少,第一种创建了String类的常量,会形成常量池,请求较多则会直接访问常量池,能节省时间。但是第一次请求时第二种方式较快。