-1

What is the best way to check if d1.getDept() and d2.getDept() are null in the following lambda before doing the compare? Should the null check be done before the Collections.sort() or can it be done within the lambda statement?

if (sortBy.contains("Dept")) {
            Collections.sort(inventoryVariances, (d1, d2) -> d1.getDept().compareTo(d2.getDept()));
        }

I need to ensure the Dept data is not null for each object prior to trying to sort by Dept.

  • 3
    have u try filtring before sorting – DEV Jun 26 '21 at 18:43
  • Is the in-place sorting essential? --- What should happen if `null`s are encountered? – Turing85 Jun 26 '21 at 18:44
  • @Hamza great point, I should filter out nulls or empty data prior to sorting – halfpenny-ian Jun 26 '21 at 18:46
  • @Turing85 is essential in this case for in-place sorting. However, can filter out nulls prior to sorting – halfpenny-ian Jun 26 '21 at 18:46
  • How is that suppose to work? If we filter-out `null`s, we cannot use in-place sorting since we channge the input (by filtering out the `null`s). We would have to either move all `null`s to the front or to the back. – Turing85 Jun 26 '21 at 18:49
  • @Turing85 Good point I am not sure, do you have a solution? – halfpenny-ian Jun 26 '21 at 18:50
  • That totally depends on the domain. Without knowing the domain, I would have to assume, which I would like to avoid. Even if I know the domain, I might not be able to make an educated guess. – Turing85 Jun 26 '21 at 18:51
  • 1
    In case we want to have `null`s first/last, [this question](https://stackoverflow.com/questions/29104069/how-to-sort-collection-with-nulls-and-invert-the-list-afterwards) by [Jeremy](https://stackoverflow.com/users/824142/jeremy) might be a duplicate. – Turing85 Jun 26 '21 at 18:55
  • 1
    Decide, what should happen with the `null` values, then use either `inventoryVariances.sort(Comparator.comparing(TheElementType::getDept, Comparator.nullsFirst(Comparator.naturalOrder())));` or `nullsLast`. – Holger Jun 28 '21 at 11:20

2 Answers2

0

you can try filtring empty values :

inventoryVariances
.stream()
.filter(c -> c.getDept() !=null)
.sort((d1, d2) -> d1.getDept().compareTo(d2.getDept())
DEV
  • 1,607
  • 6
  • 19
0

Duplicate of Dealing with a null attribute using java 8 streams and sorting using lambda expressions

Straight answer is "no" the collections does not support null values. The use of java 8 comparator nullfirst and nullLast also can be a tricky case for collections as the documentation suggests: "If the specified comparator is null, then the returned comparator considers all non-null values to be equal."

The simplest way though would be to filter out nulls first: Assuming that your collection on which you are sorting is an arraylist and you are converting it to another sorted list you can do something like:

inventoryVariances.stream().filter(d -> d.getDept() !=null).sorted(Variance::getDept).collect(Collectors.toCollection(LinkedList::new ));
    
CVS
  • 41
  • 6
  • 3
    If it is a duplicate close-vote, don't answer. – Turing85 Jun 26 '21 at 19:02
  • "*"If the specified comparator is null, then the returned comparator considers all non-null values to be equal."*" - That is what [`Comparator::thenComparing`](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/Comparator.html#thenComparing(java.util.Comparator)) is for. – Turing85 Jun 26 '21 at 19:06
  • Just added a bit of more clarification for the reader to have an understanding of theconcept. – CVS Jun 26 '21 at 19:06
  • @Turing85 ..for then comparing which has to be used with another comparator and also throws a NPE if argument is null, can you please elaborate on a case when two nulls are compared. – CVS Jun 26 '21 at 19:11
  • @Hamza ,please refrain from using lambdas where Method references can be directly used. Using lambda expression will only cause one more method call to the generating method from lambda. There are a lot of references on the internet to read about that. – CVS Jun 26 '21 at 19:16
  • Huh true, using `thenComparing` is wrong. It can be done throuhg nesting if the object itself can also be null. [This Ideone demo](https://ideone.com/O1yG84) should cover all cases. – Turing85 Jun 26 '21 at 19:33
  • 1
    `sorted` expects a `Comparator`, so `.sorted(Variance::getDept)` does not work. Further, there is no reason to demand a `LinkedList` here. It’s also unclear why you say “assuming that your collection on which you are sorting is an arraylist…”, as this code does not depend on any specific source list type. – Holger Jun 28 '21 at 11:25
  • 1
    @Turing85 it’s not mentioned in the documentation, but the special null comparators override the `thenComparing` method to do the right thing, i.e. to not delegate when the arguments are `null`. So, using `thenComparing` would work as well, see [this modified variant of your demo](https://ideone.com/TcHIPR). – Holger Jun 28 '21 at 11:37