• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Errata for OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide - page 223

 
Ranch Hand
Posts: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now this must be the most complicated piece of code I've ever set eyes on - ouch, my head hurts!

The problem is: the code below compiles, but throws a runtime exception.

After experimenting with it, I have found that replacing the call to Comparator.naturalOrder() with an implementation of Comparator which specifies the generic type (Character) works:

Now replacing the lambda with:

also works and returns the expected result:


Since compareTo() is called on the instance, it will be called on a Character and use a Character argument, so the first error is not thrown. The problem is: Comparable.compareTo() has a different signature to Comparator.compare(). Now what is going on?? Looking at the API, Collectors.minBy() doesn't look like it's overloaded.
 
author & internet detective
Posts: 42101
933
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It runs fine on my machine. I wonder if it depends on the version of the JDK?
 
T Vergilio
Ranch Hand
Posts: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's bizarre. Correction: it doesn't even compile. (IntelliJ doesn't seem to be aware of this though, as it didn't highlight it)

I'm running JDK 1.8.0_60.

I thought it could have been an issue with IntelliJ, so I tried compiling it from the command line and got the same error:

 
T Vergilio
Ranch Hand
Posts: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't work with the latest JDK 1.8.0_71 either.

This seems to be the same issue as this one.
 
Jeanne Boyarsky
author & internet detective
Posts: 42101
933
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I replied in the other thread as well. It definitely depends on the version of the JDK. And the one it worked with was old!
 
We don't have time to be charming! Quick, read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic