我正在尝试使用lambda来平均另一个分组字段上的字段。它失败了,并表示无法找到现场价格。它确实找到了tradeMinutes字段没有问题。这会让我相信它现在确实是正确的课程。
eclipse中的编译错误
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
price cannot be resolved or is not a field
at com.simplegrowth.assets.AssetAdder.<init>(AssetAdder.java:60)
at com.simplegrowth.Reader.<clinit>(Reader.java:23)
groupList功能
List<Asset> assetsList = null;
assetsList = assets.get("BILL");
assetsList.stream()
.collect(Collectors.groupingBy( a -> a.tradeMinutesSinceMidnight, Collectors.averagingDouble( a -> a.price) ) )
.forEach(( tradeMinutesSinceMidnight, sumPrice) -> System.out.println(tradeMinutesSinceMidnight + " " + sumPrice));
资产类
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) {
if (sequenceNo == ((Asset)obj).sequenceNo)
return true;
return false;
//return super.equals(obj);
}
@Override
public int compareTo(Asset otherAsset) {
if (this.getSequenceNo() < otherAsset.getSequenceNo()) {
return -1;
}
return 1;
}
}