java: 实体Entity写法+clone

1. 实体类和数据库结构有关,简单写法 + clone

package com.ycyj.common.entity.gongshi;


import lombok.Data;
import lombok.ToString;

@Data  // 加上無需重写toString
public class CapitalBarEntity implements Cloneable {

    // 跟庄资金 大单资金
    public String Code = "";
    public String Date;
    public int Period;
    public long Time;
    public double JingLiuRu;
    public double SuperJLR;
    public double LargeJLR;
    public double SuperBuy;
    public double SuperSell;
    public double LargeBuy;
    public double LargeSell;

    // 無参构造器默认存在,有参构造器如果需要就写

    @Override
    public CapitalBarEntity clone()  {
        try {
            return (CapitalBarEntity) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return this;
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        //CapitalBarEntity c = new CapitalBarEntity();
        //CapitalBarEntity c1 = c.clone();
        //System.out.println("c == c1 : " + (c == c1));

        //CapitalBarEntity c1 = new CapitalBarEntity().clone();
        //System.out.println("c == c1 : " + (c == c1));

    }

}

  

 

2 clone的使用方法

        ArrayList<CapitalBarEntity> rows = new     CapitalMinutesTable().find_rows(code, 10001, st, et);
        

        // 复制为少字段的数据字典列表, 方法1, 深度复制
        var copy_rows = rows.stream().map(CapitalBarEntity::clone).toList();

        // 复制为少字段的数据字典列表, 方法2, 深度复制
        ArrayList<CapitalBarEntity> copy_rows = new ArrayList<>();
        for(var item: rows){
            copy_rows.add(item.clone());
        }
        
        copy_rows.get(0).Time = 12210000;
        log.debug(String.valueOf((rows.get(0).Time == r.get(0).Time)));
        log.debug("rows: {} {}", rows.size(), rows.get(0));
        log.debug("copy_rows: {} {}", copy_rows.size(), copy_rows.get(0));    

  

 

posted @ 2021-12-31 17:33  Adamanter  阅读(201)  评论(0)    收藏  举报