• 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 ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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

 
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.
 
author
Posts: 232
27
  • 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!