You can use method reference in this case:
List<Integer> f = new LinkedList<>();
Collections.sort(f, Integer::compare);
In the original code there is missing return statement:
Collections.sort(f, (f1 , f2) -> {
return Integer.compare(f1,f2);
});
return must be used if lambda contains {}
Same thing without return and brackets:
Collections.sort(f, (f1 , f2) ->
Integer.compare(f1,f2)
);
A few useful notes from comments section below:
It is possible to just use Collections.sort(f) and rely on natural ordering. by Jean-François Savard
Since Java 8 List interface has sort method which can also be used f.sort(null); f.sort(Comparator.naturalOrder()); or, Collections.sort(f, Comparator.naturalOrder()); by Holger
LinkedListmost likely haven’t read it…Lists, where appropriate. But preferArrayList. There is almost never a reason forLinkedListnorVector.