Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Core Java for the Impatient - Chapter 8 Streams question  RSS feed

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Chapter 8, there's a section called How Not To Work With Optional values.

Can you summarize in one sentence what are the gotchas to watch out for when designing and using APIs that exposes optional values ?

As far as I'm concerned, nulls in the JVM is an impediment to using java.util.Optional ? It applies to Enums as well.

P.S. The book contains too much introductory material for me to fork out the money for it.
 
Cay Horstmann
author
Ranch Hand
Posts: 172
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, if you already know Java, then Core Java for the Impatient isn't for you. To quickly cut to the chase of Java 8, check out Java SE 8 for the Really Impatient (http://horstmann.com/java8/)

For an API designer, there isn't much mystery about Optional. Just return an Optional<T> instead of a "T or null".

For the consumer of such an API, the key insight is not to use the isPresent or get methods. Instead, use orElse, ifPresent, or chain a consuming function with flatMap.

Cheers,

Cay
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I'll take a look at "Java 8 for the Really Impatient".
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!