2015-05-04 91 views
-1

我有以下查詢,我想將其轉換爲在我擁有的List類上使用。目標是將其轉換爲java 8 lambda。我正面臨的主要問題是要將「trademinuessincetimenight」和「paper」輸出到新列表中。AssetTotals類當然會包含帶訪問器的4個輸出字段(price,countPapers,paper,trademinutessincemidnight)。將SQL轉換爲java lambda

SELECT avg(price), count(price), paper, TRADEMINUTESSINCEMIDNIGHT FROM sgdata.ASSET group by TRADEMINUTESSINCEMIDNIGHT, paper order by paper, TRADEMINUTESSINCEMIDNIGHT 

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; 
    } 
    @Override 
    public String toString() { 
     return this.paper + " " + this.price + " " + this.tradeMinutesSinceMidnight; 

    } 

} 
+3

在大多數情況下,您最好讓數據庫爲您執行查詢,而不是試圖將更多數據檢索到VM中,然後執行Java代碼執行搜索和過濾。你想做什麼,爲什麼? –

+0

如果我有數據庫中的信息,我會在數據庫中完成它。但是,我正在實時使用一個列表並添加到該列表中。不將數據寫入數據庫並檢索它的聚合。但我知道如何使用數據庫,這就是爲什麼我寫了它看起來像。 – user2130951

+1

你可以用你的lambda表達式顯示你已經嘗試過的東西嗎?如果你能夠證明自己的表情到目前爲止還有多遠,並且對錶達的某一部分提出具體的幫助,那麼你就更有可能從別人那裏得到答案,而不是要求某人爲你寫表達方式? –

回答

3
List<Asset> list=…; 
Comparator<Asset> group=Comparator.comparing(Asset::getPaper) 
            .thenComparing(Asset::getTradeMinutesSinceMidnight); 
list.stream().collect(Collectors.groupingBy(
     Function.identity(),()->new TreeMap<>(group), 
     Collectors.summarizingDouble(Asset::getPrice))) 
    .forEach((a,p)-> System.out.println(
      a.getPaper() 
     +"\t"+a.getTradeMinutesSinceMidnight() 
     +"\t"+p.getAverage() 
     +"\t"+p.getCount())); 

因爲要分組和排序相同的屬性,實現這一目標的最簡單的方法是通過收集成採用適當的Comparator有序映射。生成的映射將具有Asset實例作爲這些屬性具有不同元組的鍵,但不允許對其他屬性做出任何假設,因爲鍵對象只是從組中選取的一個任意實例。

地圖的值由下游收集器,這是一個summarizingDouble(Asset::getPrice)集電極將產生的DoubleSummaryStatistics實例保持所有聚合的值的屬性pricemaxminaveragecountsum確定。我按照您的問題要求打印了count,但是,我懷疑您實際上想要sum,但它很容易更改。

還與this answer比較,其中還討論了按一個屬性分組和聚合多個屬性的相關任務。

+0

哇,這是一種在sQL中只需要一行的複雜方法。我會盡快測試! – user2130951