• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
  • Carey Brown
  • salvin francis
  • Claude Moore

sorted() method of the Stream class in the OCJP 8 book byjeanne boyarsky and scott selikoff.  RSS feed

Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

On page 199, the sorted method uses:

However, we cannot use method reference:

As per my understanding both the implementation do the same thing then how come only the first one works and the other does not? Please help me understand this.
Master Rancher
Posts: 3189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Shahid,

indeed, I found that passage also confusing. But what I always do when I am unsure of a method reference: I replace that reference by the full lambda version, to see what I end up with.
So, let's take a look at Comparator::naturalOrder (or reversedOrder for that matter): we need a Comparator, a Comparator takes two input parameters and returns an int. So, let us replace it with the full version:

And the book rightfuly remarks that this is incorrect, since the method 'Comparator.naturalOrder()' is a method that takes no parameters! And there you have the compiler error!

But you CAN use a methodreference if you want to. Comparator.naturalOrder()  (with one dot!) returns a Comparator, that has the method: compare(a, b). So you can say:

I had to practise this quite a few times before I got the hang of it (and sometimes I still err), but if in doubt, as I said, I try to use the full version.
Ranch Hand
Posts: 3218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Note: this was written without seeing Piet's message.  So, I'm replying to the original poster here, not Piet. - Mike]

They aren't quite the same thing.  

Comparator.reverseOrder() returns a Comparator - when the code is executed, the Comparator actually exists, and a reference to that comparator is passed to... well, whatever method it's being passed to.  (Looks like there's a typo in the code you showed.)

Comparator::reverseOrder gives a reference to a method that could give you a Comparator.  If someone were to invoke that method.  Which they haven't, yet.

The first method can be used like this:

While the second method requires something more to actually get a Comparator from the reference:
That feels good. Thanks. Here's a tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!