拷贝list集合中对象的字段

需要从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类的常量,会形成常量池,请求较多则会直接访问常量池,能节省时间。但是第一次请求时第二种方式较快。

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,这是一个比较典型的Java IO操作题目,我将会提供代码实现,但是需要你自己去创建项目,以及拷贝input.txt文件到项目根目录下。 首先是Student类的定义,如下所示: ```java package xue; import java.util.Date; public class Student implements Comparable<Student> { private String id; private String name; private Date birthdate; private String gender; public Student(String id, String name, Date birthdate, String gender) { this.id = id; this.name = name; this.birthdate = birthdate; this.gender = gender; } // getter and setter methods @Override public int compareTo(Student o) { return this.birthdate.compareTo(o.birthdate); } @Override public String toString() { return id + " " + name + " " + birthdate.toString() + " " + gender; } } ``` 这里我们使用了Java的Date类来表示学生的出生日期,同时实现了Comparable接口,以便后续对学生集合进行排序。 接下来是主类Text27的实现,如下所示: ```java package xue; import java.io.*; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; public class Text27 { public static void main(String[] args) { List<Student> list = new ArrayList<>(); try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"))) { String line = null; while ((line = reader.readLine()) != null) { String[] values = line.split(" "); String id = values[0]; String name = values[1]; Date birthdate = new SimpleDateFormat("yyyy-MM-dd").parse(values[2]); String gender = values[3]; Student student = new Student(id, name, birthdate, gender); list.add(student); } } catch (IOException | ParseException e) { e.printStackTrace(); } Collections.sort(list); try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) { for (Student student : list) { writer.write(student.toString()); writer.newLine(); } } catch (IOException e) { e.printStackTrace(); } } } ``` 这里我们使用了Java的IO类,通过BufferedReader和FileReader来读取input.txt文件,同时使用了BufferedWriter和FileWriter来输出到output.txt文件。在读取每一行内容时,我们使用了String类的split方法来将每一行字段拆分开来,再通过SimpleDateFormat类来解析出生日期,最后构造出Student对象并加入到集合。 排序时,我们使用了Collections类的sort方法,并传入一个实现了Comparable接口的Student对象列表,这样可以按照出生日期从早到晚排序。 最后,我们使用BufferedWriter将排序后的学生集合输出到output.txt文件,并按照题目要求的格式输出每一行学生的信息。 希望能够帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值