拷贝list集合中对象的字段

本文介绍了一种在Java中将一个列表中的对象拷贝到另一个列表的方法,具体为如何将List<SysMessage>中的数据复制到List<Message>中。探讨了两种实现方式:一种是通过JSON转换,另一种是利用BeanUtils工具类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

 

<think>好的,用户现在有一个需求,要将一个包含90万对象List转换成List<Map>,并且要求高效率。我需要仔细考虑这个问题,找出最优化的方法。 首先,我需要明确用户的具体需求。用户提到的“90万的list对象集合list map集合”,可能是指将List中的每个对象转换为一个Map,其中每个Map的键值对对应对象的属性。比如,一个User对象可能有id、name等属性,转换后每个User对象变成Map的键值对,如{"id":1, "name":"Alice"}。目标是将这个转换过程尽可能高效,减少时间和资源消耗。 接下来,我需要分析可能影响效率的因素。处理90万的数据量,任何小的效率提升都会累积起来。主要考虑以下几个方面: 1. **初始容量设置**:ArrayList在扩容时会导致数组复制,影响性能。如果预先设置合适的初始容量,可以减少扩容次数。例如,假设原始List的大小是90万,那么新的List也应初始化为同样的大小,避免多次扩容。 2. **对象转换方式**:使用反射虽然灵活,但性能较差。如果用户能通过getter方法直接获取属性,或者直接访问字段,会更快。但需要看对象的结构是否允许。 3. **并行流处理**:Java 8的并行流可以利用多核处理器,加快处理速度。但需要考虑线程安全和同步开销,如果每个元素的处理是独立的,并行流可能有效。 4. **避免不必要的操作**:例如,在循环中创建不必要的对象,或者重复计算,应该尽量减少。 接下来,我需要给出具体的实现方案。假设用户的对象有明确的getter方法,可以采用以下步骤: 1. **预先初始化List的大小**:使用ArrayList的构造函数指定初始容量,避免扩容。 2. **使用高效的遍历方式**:比如for循环比增强型for循环稍快,但差异不大。或者使用流式处理,尤其是并行流。 3. **优化单个对象的转换**:在转换每个对象到Map时,尽可能直接调用getter方法,而不是用反射。例如,手动将每个属性put到Map中。 示例代码可能如下: ```java List<MyObject> originalList = ...; // 原始List,假设有90万元素 List<Map<String, Object>> resultList = new ArrayList<>(originalList.size()); for (MyObject obj : originalList) { Map<String, Object> map = new HashMap<>(); map.put("id", obj.getId()); map.put("name", obj.getName()); // 添加其他属性 resultList.add(map); } ``` 如果属性较多,手动编写每个put语句可能繁琐,但这是最高效的方式。如果属性很多,可以考虑使用反射,但需要缓存反射相关的元数据,比如Field或Method对象,避免每次循环都进行查找。 另一个优化点是使用更快的Map实现,比如提前指定HashMap的初始容量和负载因子,减少resize次数。例如,如果每个Map有4个属性,可以初始化容量为(4/0.75 +1)=6,即new HashMap<>(6)。 如果考虑并行处理,可以使用并行流: ```java List<Map<String, Object>> resultList = originalList.parallelStream() .map(obj -> { Map<String, Object> map = new HashMap<>(); map.put("id", obj.getId()); map.put("name", obj.getName()); // 其他属性 return map; }) .collect(Collectors.toList()); ``` 但需要注意,并行流在数据量大时可能更高效,但需要测试实际效果,因为线程切换和合并结果也会有开销。 另外,可以考虑使用对象池或重用Map对象,但必须确保线程安全,这在并行流中可能不可行,因为每个线程处理自己的元素。 此外,如果原始数据是只读的,可以考虑其他数据结构或更高效的序列化方式,但可能在当前问题中不适用。 可能的问题: - 原始对象的属性数量:如果属性很多,手动写put语句麻烦,如何解决? - 是否允许使用第三方库,如Apache Commons BeanUtils或Spring的BeanMap,但这些库可能使用反射,效率不如直接调用方法。 - 内存使用情况:90万Map对象会占用较多内存,可能需要考虑内存优化,比如使用更紧凑的数据结构或序列化。 最终,最有效的方法还是预先初始化List大小,直接调用getter方法,并尽可能减少每次循环中的开销。</think><< 将包含90万对象List高效转换为List<Map>,可以通过以下方法实现: 1. **预初始化集合容量** 使用`new ArrayList<>(initialCapacity)`预先指定List容量,避免动态扩容带来的性能损耗。 ```java List<MyObject> originalList = ...; // 原始数据 List<Map<String, Object>> resultList = new ArrayList<>(originalList.size()); ``` 2. **直接属性访问** 通过直接调用对象的getter方法填充Map,避免反射带来的性能损失: ```java for (MyObject obj : originalList) { Map<String, Object> map = new HashMap<>(4); // 根据属性数量初始化容量 map.put("id", obj.getId()); map.put("name", obj.getName()); // 其他属性... resultList.add(map); } ``` 3. **并行流加速** 使用Java 8并行流利用多核CPU(需确保操作是线程安全的): ```java List<Map<String, Object>> resultList = originalList.parallelStream() .map(obj -> { Map<String, Object> map = new HashMap<>(); map.put("id", obj.getId()); map.put("name", obj.getName()); // 其他属性... return map; }) .collect(Collectors.toList()); ``` **优化原理**: - **容量预分配**:ArrayList扩容需要复制数组,预分配可减少90万次潜在的数据拷贝。 - **避免反射**:直接调用getter方法比反射(如BeanUtils)快10-100倍。 - **并行处理**:并行流将任务分割到多个CPU核心,适合CPU密集型操作。 **注意事项**: - 如果对象属性较多,手动编写put语句可能冗长,但性能最优。 - 并行流可能因线程竞争导致性能下降,需实际测试验证效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值