string - Java 8 Comparator nullsFirst naturalOrder confused -



string - Java 8 Comparator nullsFirst naturalOrder confused -

this may simple question understand clearly...

i have code this:

public final class persona { private final int id; private final string name public persona(final int id,final string name) { this.id = id; this.name = name; } public int getid(){return id;} public string getname(){return name;} @override public string tostring(){return "persona{" + "id=" + id + ", name=" + name+'}';} }

and testing code:

import static java.util.comparator.*; private void nullsfirsttesting() { final comparator<persona>comparator = comparing(persona::getname,nullsfirst(naturalorder())); final list<persona>persons = arrays.aslist(new persona(1,"cristian"),new persona(2,"guadalupe"),new persona(3,"cristina"),new persona(4,"chinga"),new persona(5,null)); persons .stream() .sorted(comparator) .foreach(system.out::println); }

this shows next results:

persona{id=5, name=null} persona{id=4, name=chinga} persona{id=1, name=cristian} persona{id=3, name=cristina} persona{id=2, name=guadalupe}

these results ok me have problem understanding.

when ignore new persona(5,null) object , pass comparator:

final comparator<persona>comparator = comparing(persona::getname);

it works charm. sorting natural order of name property. problem arises when add together object name=null, thought need comparator this.

final comparator<persona>comparator = comparing(persona::getname,nullsfirst());

my thought erroneous: "ok, when name non-null, sorted in natural order of name, previous comparator, , if null first but non-null names still sorted in natural order".

but right code this:

final comparator<persona>comparator = comparing(persona::getname,nullsfirst(naturalorder()));

i don't understand parameter nullsfirst. thought natural order of name explicitly [default] handle null values.

but docs say:

returns null-friendly comparator considers null less non-null. when both null, considered equal. if both non-null, specified comparator used determine order. if specified comparator null, returned comparator considers non-null values equal.

this line: "if both non-null, specified comparator used determine order."

i confused when , how natural order should explicitly set or when inferred.

the "natural order" comparator, when utilize comparing 1 parameter, not handle nulls. (i'm not sure got thought did.) "natural order" of comparable class defined compareto() method, used this:

obj1.compareto(obj2)

obviously won't work if obj1 null; string, throw exception of obj2 null.

the naturalorder() method returns comparator compares 2 objects. javadoc explicitly says comparator throws nullpointerexception when comparing null.

the nullsfirst() method (and nullslast() similarly) transforms comparator new comparator. set in comparator may throw exception if tries compare null, , spits out new comparator works same way except allows null arguments. that's why need parameter nullsfirst--because builds new comparator on top of existing comparator, , tell existing comparator is.

so why doesn't give natural order if leave out parameter? because didn't define way. nullsfirst defined in javadoc take parameter:

static <t> comparator<t> nullsfirst(comparator<? super t> comparator)

i think if designers wanted to, have added overload takes no parameters:

static <t> comparator<t> nullsfirst() // note: not legal

that same using nullsfirst(naturalorder()). didn't, can't utilize that.

java string sorting java-8 comparator

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -