• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Issue in Supplier<String> in Optional orElseGet method

 
Ranch Hand
Posts: 163
1
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am finding issue with Supplier in Optional and code is as below:


Could you please throw any light on the above issue

Thanks & Regards,

Swapna
 
Sheriff
Posts: 3914
542
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is your issue occurring at while compiling or at run-time?

Please post any error/warning messages.
 
Saloon Keeper
Posts: 14268
321
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is likely that Optional.ofNullable(null) returns an Optional<Object>, so calling the orElseGet() method will return an Object, not a String.

Instead of calling Optional.ofNullable(null), use Optional.empty().
 
Marshal
Posts: 76415
365
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Optional isn't simply called Optional but Optional<T>. As Stephan says, if you simply pass null, that has no type associated with it, so the only type the compiler could use would be Object. You can consider casting the value returned from orElseGet() to a String, or you can cast null (probably better). Any of the following declarations works, but the cast is always inelegant.Rather more elegant:-Because both the Optional#empty() and ofNullable(T) mthods are declared as static <T>, it is necessary to apply generics to the method; since you are applying generics to the method not the class, the <String> part follows the dot operator.
 
Stephan van Hulst
Saloon Keeper
Posts: 14268
321
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You only have to explicitly pass the generic type argument if you chain the orElseGet() call. If you assign the Optional to a variable first, the generic type argument is inferred from the assignment context:
 
Campbell Ritchie
Marshal
Posts: 76415
365
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Stephan I tried that on JShell and it worked nicely.
 
Swapna latha
Ranch Hand
Posts: 163
1
jQuery Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Stephan. You are the best.
 
Swapna latha
Ranch Hand
Posts: 163
1
jQuery Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Optional isn't simply called Optional but Optional<T>. . . .



Campbell, the examples are very clear. Thank you very much.
 
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic