sorting - Java 8 comparator with if clause -


i want sort list of objects database date. problem location of date(table1,table2) depends on objecttype. if 1, date should picked first table, if 2, second. started this:

     objectlist.stream().sorted((h1,h2)->      if(h1.getobjecttype().getid()==1){      h1.getobject().gettable1().gettime()}//here problem      ); 

but got confused, because after if clause can't add .compareto. there way make java 8 comparator using lambda expressions if clause?

there few things going on @ once here.

the first if want use if-else statement inside of lambda, need use statement lambda (the kind braces) instead of expression lambda (in braces omitted). statement lambda, have use return statement explicitly, whereas in expression lambda return value implicitly result of evaluating expression.

you have conditional logic selecting value compared. i'll assume logic needs applied independently each of 2 objects being compared, means straightforward way proceed require write out logic twice.

finally, need write logic actual comparison.

for example i'll assume objects being sorted of type obj , values they're being sorted on of type objdate i'll further assume comparable each other. (that is, objdate implements comparable<objdate>.) i'll assume object ids either 1 or 2 won't handle case of value being else.

here's fully-written-out comparator using statement lambda:

    objectlist.stream().sorted((h1, h2) -> {         objdate h1date;         objdate h2date;          if (h1.getobjecttype().getid() == 1) {             h1date = h1.getobject().gettable1().gettime();         } else {             h1date = h1.getobject().gettable2().gettime();         }          if (h2.getobjecttype().getid() == 1) {             h2date = h2.getobject().gettable1().gettime();         } else {             h2date = h2.getobject().gettable2().gettime();         }          return h1date.compareto(h2date);     }); 

ugh! writing comparators fun, isn't it? :-)

basically idea apply get-from-table1-or-table2 logic extract right value first object, , same second object. finally, return result of comparing them.

this work, there's obvious duplicated code here. common code can referred key extractor because given object compared, extracts key that's used basis comparison. here's key extractor method single object:

objdate getsortingdate(obj obj) {     if (obj.getobjecttype().getid() == 1) {         return obj.getobject().gettable1().gettime();     } else {         return obj.getobject().gettable2().gettime();     } } 

now have this, can simplify comparator considerably:

    objectlist.stream().sorted((h1, h2) -> {         objdate h1date = getsortingdate(h1);         objdate h2date = getsortingdate(h2);         return h1date.compareto(h2date);     }); 

if collapse local variables, can convert expression lambda:

    objectlist.stream().sorted(         (h1, h2) -> getsortingdate(h1).compareto(getsortingdate(h2))); 

finally, idea of extracting key 2 objects , comparing them common comparators there's helper method you: comparator.comparing(keyextractor). given 2 objects, runs key extractor on both objects , compares them. (more precisely, returns function this.) can use directly simplify things further:

    objectlist.stream().sorted(comparator.comparing(this::getsortingdate)); 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -