第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用 Comparator.nullsLast 時(shí)出現(xiàn) NullPointerException

使用 Comparator.nullsLast 時(shí)出現(xiàn) NullPointerException

達(dá)令說(shuō) 2022-05-25 17:22:34
我有以下代碼,其中包含 2 個(gè)類 MyRange 和 MyCustomValue -class MyRange {    private Long id;    private Double minValue;    private Double maxValue;    // getters and setters    // equals, hashCode and toString    @Override    public boolean equals(Object obj) {        if (this == obj)            return true;        if (obj == null || getClass() != obj.getClass())            return false;        MyRange other = (MyRange) obj;        return Objects.equals(this.id, other.id) &&                Objects.equals(this.minValue, other.minValue) &&               Objects.equals(this.maxValue, other.maxValue);    }}class MyCustomValue {    private String value;    private MyRange myrange;    //getters and setters    // equals, hashCode and toString}如果它value是空的,MyCustomValue我希望它在最后。所以我寫(xiě)了如下的比較器public static final Comparator<MyCustomValue> externalMVComparator = (emv1, emv2) -> {    if(emv1.getValue() != null && emv2.getValue() == null) {        return -1;    } else if (emv1.getValue() == null && emv2.getValue() != null) {        return 1;    } else {        return myrangeMinValueComparator.compare(emv1, emv2);    }}private static final Comparator<MyRange> minValueComparator =  Comparator.nullsLast(Comparator.comparingDouble(value -> value.getMinValue()));private static final Comparator<MyCustomValue> myrangeMinValueComparator = Comparator.nullsLast(Comparator.comparing(MyCustomValue::getMyrange, minValueComparator));上述比較器工作正常。所以我決定改變externalMVComparator如下(即,使用thenComparing更多的可讀性)private static final Comparator<MyCustomValue> valueComparator = Comparator.nullsLast(Comparator.comparing(MyCustomValue::getValue));public static final Comparator<MyCustomValue> externalMVComparator2 = Comparator.nullsLast(valueComparator.thenComparing(myrangeMinValueComparator));但是對(duì)列表進(jìn)行排序,externalMVComparator2結(jié)果為NullPointerException. 我的代碼中有什么錯(cuò)誤?
查看完整描述

1 回答

?
白衣非少年

TA貢獻(xiàn)1155條經(jīng)驗(yàn) 獲得超0個(gè)贊

問(wèn)題出在以下行(Comparator.為清楚起見(jiàn),我刪除了):

Comparator<MyCustomValue> valueComparator = nullsLast(comparing(MyCustomValue::getValue));

Comparator您所做的將處理類型的nullMyCustomValue。它不會(huì)處理由 .null返回的 s getValue。您必須使用 2 參數(shù)版本Comparator.comparingnull為值提供 -safe 比較器:

valueComparator = comparing(MyCustomValue::getValue, nullsLast(naturalOrder()));

以上將處理您實(shí)際想要排序的常見(jiàn)情況value。當(dāng)我查看您的代碼時(shí),我認(rèn)為您的意思是只使用valuefornull檢查,否則不想按它排序。如果是這種情況,您可以將nullsLast( (x,y) -> 0) 其用作 null 安全的第二個(gè)參數(shù),因?yàn)?code>comparing它會(huì)將所有字符串視為相等。您也可以使用valueComparator = comparing(mcv -> mcv.getValue() == null)因?yàn)?code>true按false自然順序排列,但這可能不太清楚。

如果您還想處理nulls of MyCustomValue,則必須再次將其包裝nullsLast起來(lái)。


查看完整回答
反對(duì) 回復(fù) 2022-05-25
  • 1 回答
  • 0 關(guān)注
  • 569 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)