This week's book giveaway is in the JDBC and Relational Databases forum.
We're giving away four copies of Murach's MySQL and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 41851
904
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: 41851
904
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!
 
It's just a flesh wound! Or a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic