将行摘要创建为新列表<>

时间:2015-04-24 09:13:46

标签: lambda java-8

我想总结一下List中的信息来平均一个字段,并将按分钟信息分组写入List列表。

我有这个资产类,我希望使用long tradeMinutesSinceMidnight对每一分钟进行分组。分组一切。然后平均价格列。写其他字段不变。它们在每一行都是一样的。

我认为这可以在java 8中使用lambda表达式实现。

public class Asset implements Serializable, Comparable<Asset> {

    private static final long serialVersionUID = 1L;

    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id; // still set automatically

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    long sequenceNo;
    String Exchange;
    String Board;
    long time;
    String paper;
    long tradeTime;
    long quantity;
    double price;
    String source;
    String buyer;
    String seller;
    float changeSinceLast;
    String initator;
    long tradeTimeSinceMidnight;
    long tradeMinutesSinceMidnight;
    long daysSinceEpoch;


    public long getSequenceNo() {
        return sequenceNo;
    }
    public void setSequenceNo(long sequenceNo) {
        this.sequenceNo = sequenceNo;
    }
    public String getExchange() {
        return Exchange;
    }
    public void setExchange(String exchange) {
        Exchange = exchange;
    }
    public String getBoard() {
        return Board;
    }
    public void setBoard(String board) {
        Board = board;
    }
    public long getTime() {
        return time;
    }
    public void setTime(long time) {
        this.time = time;
    }
    public String getPaper() {
        return paper;
    }
    public void setPaper(String paper) {
        this.paper = paper;
    }
    public long getTradeTime() {
        return tradeTime;
    }
    public void setTradeTime(long tradeTime) {
        this.tradeTime = tradeTime;
    }
    public long getQuantity() {
        return quantity;
    }
    public void setQuantity(long quantity) {
        this.quantity = quantity;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public String getSource() {
        return source;
    }
    public void setSource(String source) {
        this.source = source;
    }
    public String getBuyer() {
        return buyer;
    }
    public void setBuyer(String buyer) {
        this.buyer = buyer;
    }
    public String getSeller() {
        return seller;
    }
    public void setSeller(String seller) {
        this.seller = seller;
    }
    public float getChangeSinceLast() {
        return changeSinceLast;
    }
    public void setChangeSinceLast(float changeSinceLast) {
        this.changeSinceLast = changeSinceLast;
    }
    public String getInitator() {
        return initator;
    }
    public void setInitator(String initator) {
        this.initator = initator;
    }

    public long getDaysSinceEpoch() {
        return daysSinceEpoch;
    }
    public void setDaysSinceEpoch(long daysSinceEpoch) {
        this.daysSinceEpoch = daysSinceEpoch;
    }
    public long getTradeTimeSinceMidnight() {
        return tradeTimeSinceMidnight;
    }
    public void setTradeTimeSinceMidnight(long tradeTimeSinceMidnight) {
        this.tradeTimeSinceMidnight = tradeTimeSinceMidnight;
    }

    public long getTradeMinutesSinceMidnight() {
        return tradeMinutesSinceMidnight;
    }
    public void setTradeMinutesSinceMidnight(long tradeMinutesSinceMidnight) {
        this.tradeMinutesSinceMidnight = tradeMinutesSinceMidnight;
    }


    @Override
    public boolean equals(Object obj) {

        return sequenceNo == ((Asset)obj).sequenceNo;

    }

    @Override
    public int compareTo(Asset otherAsset) {
        if (this.getSequenceNo() < otherAsset.getSequenceNo()) {
            return -1;
        }
        else if (this.getSequenceNo() == otherAsset.getSequenceNo()) {
            return 0;
        }
        return 1;
    }

}

将包含结果的AssetSummary类。

public class AssetSummary implements Serializable, Comparable<AssetSummary> {

    private static final long serialVersionUID = 1L;

    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id; // still set automatically

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    String Exchange;
    long time;
    String paper;
    long quantity;
    double avgPrice;
    long tradeMinutesSinceMidnight;
    long daysSinceEpoch;


    public String getExchange() {
        return Exchange;
    }
    public void setExchange(String exchange) {
        Exchange = exchange;
    }
    public long getTime() {
        return time;
    }
    public void setTime(long time) {
        this.time = time;
    }
    public String getPaper() {
        return paper;
    }
    public void setPaper(String paper) {
        this.paper = paper;
    }
    public long getQuantity() {
        return quantity;
    }
    public void setQuantity(long quantity) {
        this.quantity = quantity;
    }
    public double getAvgPrice() {
        return avgPrice;
    }
    public void setAvgPrice(double avgPrice) {
        this.avgPrice = avgPrice;
    }
    public long getTradeMinutesSinceMidnight() {
        return tradeMinutesSinceMidnight;
    }
    public void setTradeMinutesSinceMidnight(long tradeMinutesSinceMidnight) {
        this.tradeMinutesSinceMidnight = tradeMinutesSinceMidnight;
    }
    public long getDaysSinceEpoch() {
        return daysSinceEpoch;
    }
    public void setDaysSinceEpoch(long daysSinceEpoch) {
        this.daysSinceEpoch = daysSinceEpoch;
    }
    @Override
    public int compareTo(AssetSummary otherAsset) {
        if (this.tradeMinutesSinceMidnight < otherAsset.tradeMinutesSinceMidnight) {
            return -1;
        }
        else if (this.tradeMinutesSinceMidnight == otherAsset.tradeMinutesSinceMidnight) {
            return 0;
        }
        return 1;
    }

}

0 个答案:

没有答案