我有以下查詢,我想將其轉換爲在我擁有的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;
}
}
在大多數情況下,您最好讓數據庫爲您執行查詢,而不是試圖將更多數據檢索到VM中,然後執行Java代碼執行搜索和過濾。你想做什麼,爲什麼? –
如果我有數據庫中的信息,我會在數據庫中完成它。但是,我正在實時使用一個列表並添加到該列表中。不將數據寫入數據庫並檢索它的聚合。但我知道如何使用數據庫,這就是爲什麼我寫了它看起來像。 – user2130951
你可以用你的lambda表達式顯示你已經嘗試過的東西嗎?如果你能夠證明自己的表情到目前爲止還有多遠,並且對錶達的某一部分提出具體的幫助,那麼你就更有可能從別人那裏得到答案,而不是要求某人爲你寫表達方式? –