Home > Blockchain >  Null safe Comparator for BigDecimal
Null safe Comparator for BigDecimal

Time:11-03

I want to have an null safe Comparator. But I tried many ways ii doesn't work. **getBenefitLimit **is an **BigDecimal **value there no comparing BigDecimal values in Comparator. In that case how can figure this...

My code and error Using with out null (Comparator.nullsFirst)

List<ProductBenefitResponse> list = new ArrayList<>(benefitMap.values());
list.sort(Comparator.comparing(ProductBenefitResponse::getDescription).thenComparing(ProductBenefitResponse::getBenefitLimit));

Error must be :-

java.lang.NullPointerException: null
    at java.base/java.util.Comparator.lambda$comparing$77a9974f$1(Comparator.java:469)
    at java.base/java.util.Comparator.lambda$thenComparing$36697e65$1(Comparator.java:217)
    at java.base/java.util.TimSort.binarySort(TimSort.java:296)
    at java.base/java.util.TimSort.sort(TimSort.java:239)
    at java.base/java.util.Arrays.sort(Arrays.java:1515)
    at java.base/java.util.ArrayList.sort(ArrayList.java:1750)

My code and error Using with null (Comparator.nullsFirst)

List<ProductBenefitResponse> list = new ArrayList<>(benefitMap.values());
list.sort(Comparator.nullsFirst(
        Comparator.comparing(ProductBenefitResponse::getBenefitLimit)
                .thenComparing(ProductBenefitResponse::getDescription)
));

Error must be :-

java.lang.NullPointerException: null
    at java.base/java.math.BigDecimal.compareTo(BigDecimal.java:3065)
    at java.base/java.math.BigDecimal.compareTo(BigDecimal.java:228)
    at java.base/java.util.Comparator.lambda$comparing$77a9974f$1(Comparator.java:469)
    at java.base/java.util.Comparator.lambda$thenComparing$36697e65$1(Comparator.java:216)
    at java.base/java.util.Comparators$NullComparator.compare(Comparators.java:83)
    at java.base/java.util.TimSort.countRunAndMakeAscending(TimSort.java:355)
    at java.base/java.util.TimSort.sort(TimSort.java:234)
    at java.base/java.util.Arrays.sort(Arrays.java:1515)
    at java.base/java.util.ArrayList.sort(ArrayList.java:1750)

What is wrong in this code and please give me and answer...

CodePudding user response:

Right now the nullsFirst is applied to the ProductBenefitResponse objects, not the BigDecimal values.

Try this:

Comparator<BigDecimal> bigDecimalComparator = Comparator.nullsFirst(Comparator.naturalOrder());
list.sort(
    Comparator
        .comparing(ProductBenefitResponse::getDescription)
        .thenComparing(ProductBenefitResponse::getBenefitLimit, bigDecimalComparator)
);

By specifying the null-safe comparator for the mapping, it applies to the result of the mapping.

CodePudding user response:

I Found Easy way to Do that Is this correct ? It return the value I want anyway.

List<ProductBenefitResponse> list = new ArrayList<>
(benefitMap.values());list.sort(Comparator.comparing(ProductBenefitResponse::getDescription,Comparator.nullsLast(Comparator.naturalOrder()))
                    .thenComparing(ProductBenefitResponse::getBenefitLimit,Comparator.nullsLast(Comparator.naturalOrder())));
  • Related