4

Hi I have this (what i assume to be) really trivial code:

List<Integer> f = new LinkedList<Integer>();
    Collections.sort(f, (Integer f1, Integer f2) -> {
        Integer.compare(f1,f2);
    });

However, I get the following compile-error:

Cannot convert from Comparator<Integer> to Comparator<? super T>

This isn't very helpful - what is going wrong?

Didier L
  • 18,905
  • 10
  • 61
  • 103
bharal
  • 15,461
  • 36
  • 117
  • 195
  • 4
    This is the place for the almost mandatory reference to [“When to use LinkedList over ArrayList?”](http://stackoverflow.com/q/322715/2711488) as everyone using `LinkedList` most likely haven’t read it… – Holger Jan 23 '17 at 14:26
  • I don't always use Lists, but when I do I use Vector – bharal Jan 23 '17 at 14:43
  • You should always use `List`s, where appropriate. But prefer `ArrayList`. There is almost never a reason for `LinkedList` [nor `Vector`](http://stackoverflow.com/q/1386275/2711488). – Holger Jan 23 '17 at 14:58
  • 1
    @bharal if you prefer/use Vectors, then you need to read this (I did too and learned !) : http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated – Exception_al Jan 23 '17 at 15:00
  • @Holger everyone here is *very* serious, no? – bharal Jan 24 '17 at 14:53

1 Answers1

12

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

Community
  • 1
  • 1
Anton Balaniuc
  • 10,889
  • 1
  • 35
  • 53
  • Um, but what if i just wanted to do it how i did it - is that impossible? What am i doing wrong? Thanks for the answer, but it doesn't answer my question! – bharal Jan 23 '17 at 11:42
  • thanks so much, i didn't realise i'd messed up the return {} stuff, good to know. – bharal Jan 23 '17 at 11:47
  • @bharal I updated my original answer, but the shortest way is to use method reference. – Anton Balaniuc Jan 23 '17 at 11:49
  • Thanks again - i didn't even know they existed! I asked for the clarification just because the actual problem isn't sorting Integers, i just simplified it down (hence the desire for the full method, not your initial answer( – bharal Jan 23 '17 at 11:52
  • 5
    You could simply do Collections.sort(f) and rely on natural ordering. – Jean-François Savard Jan 23 '17 at 12:36
  • 2
    Or `f.sort(null);`. Alternatively, `f.sort(Comparator.naturalOrder());` or, `Collections.sort(f, Comparator.naturalOrder());` will do as well. – Holger Jan 23 '17 at 14:30