• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Issue in Supplier<String> in Optional orElseGet method

 
Ranch Hand
Posts: 124
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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
 
Saloon Keeper
Posts: 2806
368
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 10875
235
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 66637
251
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 10875
235
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 66637
251
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Stephan I tried that on JShell and it worked nicely.
 
Swapna latha
Ranch Hand
Posts: 124
jQuery Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Stephan. You are the best.
 
Swapna latha
Ranch Hand
Posts: 124
jQuery Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!