sorting - Performance of thenComparing vs thenComparingInt - which to use? -
i have question, if i'm comparing ints, there performance difference in calling thencomparingint(my::intmethod) vs thencomparing(my::intmethod), in other words, if i'm comparing differemt types, both reference , primitive, e.g. string, int, etc. part of me wants comparing().thencomparing().thencomparing() etc, should comparing.thencomparing().thencomparingint() if 3rd call comparing int or integer value?
i assuming comparing() , thencomparing() use compareto method compare given type behind scenes or possibly ints, integer.compare? i'm assuming answer original question may involve performance in thencomparingint know int being passed in, whereas, thencomparing have autobox int integer maybe cast object?
also, question whilst think of - there way of chaining method references, e.g. song::getartist::length getartist returns string? reason wanted this:
songlist.sort( comparator.comparing((song s) -> s.getartist().length())); songlist.sort( comparator.comparing(song::getartist, comparator.comparingint(string::length))); songlist.sort( comparator.comparing(song::getartist, string::length));
of 3 examples, top 2 compile bottom seems throw compilation error in eclipse, have thought 2nd argument of string::length valid? maybe not it's expecting comparator not function?
question 1
i think thencomparingint(my::intmethod)
might better since should avoid boxing, have try out both versions see if makes difference.
question 2
songlist.sort( comparator.comparing(song::getartist, string::length));
is invalid because 2nd parameter should comparator
not method returns int.
Comments
Post a Comment