java - Comparator.comparing(...) throwing non-static reference exception while taking String::compareTo -
below 2 lines of code snippet:
list<string> listdevs = arrays.aslist("alvin", "alchemist", "brutus", "larsen", "jason", "kevin"); listdevs.sort(comparator.comparing(string::length)); //this works fine listdevs.sort(string::comparetoignorecase); //this works fine but (out of expermient) when try write
listdevs.sort(comparator.comparing(string::comparetoignorecase)); the compiler throws error
cannot make static reference non-static method comparetoignorecase(string) type string
similar happens below code
listdevs.sort(comparator.comparing(string::compareto)); i understand error , works fine if remove comparator.comparing (as shown above).
but point is, how line works?
listdevs.sort(comparator.comparing(string::length));
i believe missing something. have read this thread. same scenario?
comparator.comparing expects function describes comparable property of elements. string::length sufficient length() property of string evaluating string int (that’s why comparingint preferable here).
in contrast, string.comparetoignorecase , string.compareto comparison methods. compare 2 string objects. references them sufficient comparator expected, not property function expected.
it’s have factory saying “gimme engine, , build car you” , trying give them complete car. while existing car valid car expected, there no sense in passing factory built car.
unfortunately, current compiler implementation bad @ reporting error functional signatures. see messages “cannot make static reference non-static method …” when signatures mismatch.
Comments
Post a Comment