簡體   English   中英

Java 按兩個 BigDecimal 之間的差值排序

[英]Java sorting by spread between two BigDecimal

請Java幫忙排序。我有一個簡單的例子(看起來像這樣)。 我需要根據兩個 BigDecimals 之間的差異對列表進行排序。

我的數據 class(我剪了吸氣劑)

    public class Quotation {
    private final long id;
    private final BigDecimal askPrice;
    private final BigDecimal bidPrice;

    public Quotation(long id, BigDecimal bidPrice, BigDecimal askPrice) {
        this.id = id;
        this.askPrice = askPrice;
        this.bidPrice = bidPrice;
    }
    }

我們在這里存儲我們的記錄。

        storeQuotation(new Quotation(1000,new BigDecimal("99"), new BigDecimal("104")));
        storeQuotation(new Quotation(1001,new BigDecimal("69"), new BigDecimal("72")));
        storeQuotation(new Quotation(1002,new BigDecimal("65"), new BigDecimal("69")));
        storeQuotation(new Quotation(1003,new BigDecimal("70"), new BigDecimal("71")));
        storeQuotation(new Quotation(1004,new BigDecimal("71"), new BigDecimal("73")));
        storeQuotation(new Quotation(1005,new BigDecimal("90"), new BigDecimal("95")));
        storeQuotation(new Quotation(1006,new BigDecimal("92"), new BigDecimal("93")));
        storeQuotation(new Quotation(1007,new BigDecimal("94"), new BigDecimal("98")));
        storeQuotation(new Quotation(1008,new BigDecimal("90"), new BigDecimal("92")));
        storeQuotation(new Quotation(1009,new BigDecimal("92"), new BigDecimal("95")));
out - Not Sorted
    id - 1000.   Bid - 99.   Ask - 104.   Spread = 5
    id - 1001.   Bid - 69.   Ask - 72.   Spread = 3
    id - 1002.   Bid - 65.   Ask - 69.   Spread = 4
    id - 1003.   Bid - 70.   Ask - 71.   Spread = 1
    id - 1004.   Bid - 71.   Ask - 73.   Spread = 2
    id - 1005.   Bid - 90.   Ask - 95.   Spread = 5
    id - 1006.   Bid - 92.   Ask - 93.   Spread = 1
    id - 1007.   Bid - 94.   Ask - 98.   Spread = 4
    id - 1008.   Bid - 90.   Ask - 92.   Spread = 2
    id - 1009.   Bid - 92.   Ask - 95.   Spread = 3

我只需要根據 bidPrice 和 askPrice 之間的差異對這個列表進行排序。 我試過這個方法...

    public static List<Quotation> getSpreadsList(boolean decreasing) {
        List<Quotation> sortedBySpread = QuotationsStoreImpl.quotationList;

        Collections.sort(sortedBySpread, (a, b) ->
     (a.getBidPrice().intValue() - b.getAskPrice().intValue()));

//        sortedBySpread.sort((a, b) -> 
//    (a.getBidPrice().intValue() - b.getAskPrice().intValue()));

        if (decreasing) {
            Collections.reverse(sortedBySpread);
        }
        return sortedBySpread;
    }
    }

但是沒有成功...

out - Sorted
    id - 1002.   Bid - 65.   Ask - 69.   Spread = 4
    id - 1003.   Bid - 70.   Ask - 71.   Spread = 1
    id - 1004.   Bid - 71.   Ask - 73.   Spread = 2
    id - 1001.   Bid - 69.   Ask - 72.   Spread = 3
    id - 1008.   Bid - 90.   Ask - 92.   Spread = 2
    id - 1009.   Bid - 92.   Ask - 95.   Spread = 3
    id - 1006.   Bid - 92.   Ask - 93.   Spread = 1
    id - 1007.   Bid - 94.   Ask - 98.   Spread = 4
    id - 1005.   Bid - 90.   Ask - 95.   Spread = 5
    id - 1000.   Bid - 99.   Ask - 104.   Spread = 5

該列表是混合的,但未根據我的標准排序! 傳播未分類!

我怎樣才能按傳播正確地排序這個列表?

我在 java 方面經驗不多。而且我所有的嘗試都沒有結果。

Collections.sort(sortedBySpread, (a, b) -> (a.getBidPrice().intValue() - b.getAskPrice().intValue())); 做一些沒什么用的數學運算,因為它從兩個不同報價的出價中減去要價。

相反,您應該計算a的傳播,然后減去b的傳播:

Collections.sort(sortedBySpread, (a, b) -> (a.getBidPrice().intValue() - a.getAskPrice().intValue()) - (b.getBidPrice().intValue() - b.getAskPrice().intValue()));

通常,這可以擴展為以下內容,以更清楚地說明發生了什么:

Collections.sort(sortedBySpread, (Quotation a, Quotation b) -> {
    int spreadA = a.getBidPrice().intValue() - a.getAskPrice().intValue();
    int spreadB = b.getBidPrice().intValue() - b.getAskPrice().intValue();
    return spreadA - spreadB;
});

但是從第一個片段開始,IntelliJ 提出了可以說是更簡潔的解決方案

Collections.sort(sortedBySpread, Comparator.comparingInt(a -> (a.getBidPrice().intValue() - a.getAskPrice().intValue())));

從那里開始,在Quotation上有一個getSpread可能是有意義的:

public int getSpread() {
    return bidPrice.intValue() - askPrice.intValue();
}

這將允許

Collections.sort(sortedBySpread, Comparator.comparingInt(Quotation::getSpread));

最后

sortedBySpread.sort(Comparator.comparingInt(Quotation::getSpread));

不需要Collections.sort

對於演示,我將您的值放在列表中。 我還將以下內容添加到您的 class。

public BigDecimal getSpread() {
       return askPrice.subtract(bidPrice);
}
public String toString() {
       return "id - %d    bid - %6.2f     ask - %6.2f     spread - %6.2f".formatted(id, bidPrice, askPrice,getSpread());   
}

數據

 List<Quotation> quotes = new ArrayList<>(List.of(
   (new Quotation(1000,new BigDecimal("99"), new BigDecimal("104"))),
   (new Quotation(1001,new BigDecimal("69"), new BigDecimal("72"))),
   (new Quotation(1002,new BigDecimal("65"), new BigDecimal("69"))),
   (new Quotation(1003,new BigDecimal("70"), new BigDecimal("71"))),
   (new Quotation(1004,new BigDecimal("71"), new BigDecimal("73"))),
   (new Quotation(1005,new BigDecimal("90"), new BigDecimal("95"))),
   (new Quotation(1006,new BigDecimal("92"), new BigDecimal("93"))),
   (new Quotation(1007,new BigDecimal("94"), new BigDecimal("98"))),
   (new Quotation(1008,new BigDecimal("90"), new BigDecimal("92"))),
   (new Quotation(1009,new BigDecimal("92"), new BigDecimal("95")))));

排序部分很簡單。 只需使用一個比較器,參考價差。

quotes.sort(Comparator.comparing(Quotation::getSpread));

並打印

for(Quotation q : quotes) {
    System.out.println(q);
} 

印刷

id - 1003    bid -  70.00     ask -  71.00     spread -   1.00
id - 1006    bid -  92.00     ask -  93.00     spread -   1.00
id - 1004    bid -  71.00     ask -  73.00     spread -   2.00
id - 1008    bid -  90.00     ask -  92.00     spread -   2.00
id - 1001    bid -  69.00     ask -  72.00     spread -   3.00
id - 1009    bid -  92.00     ask -  95.00     spread -   3.00
id - 1002    bid -  65.00     ask -  69.00     spread -   4.00
id - 1007    bid -  94.00     ask -  98.00     spread -   4.00
id - 1000    bid -  99.00     ask - 104.00     spread -   5.00
id - 1005    bid -  90.00     ask -  95.00     spread -   5.00

      

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM