Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6819035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:15:02+00:00 2026-05-26T21:15:02+00:00

Does Guava provide a method to get a default value if a passed object

  • 0

Does Guava provide a method to get a default value if a passed object reference is null ? I’am looking for something like <T> T nullToDefault(T obj, T default), were the default is returned if obj is null.

Here on stackoverflow I found nothing about it. I am only looking for a pure Guava solution (if there is some)!

I found nothing in the Gauva 10 API, only com.google.common.base.Objects looks promising but lacks something similar.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:15:02+00:00Added an answer on May 26, 2026 at 9:15 pm

    In additon to Objects.firstNonNull, Guava 10.0 added the Optional class as a more general solution to this type of problem.

    An Optional is something that may or may not contain a value. There are various ways of creating an Optional instance, but for your case the factory method Optional.fromNullable(T) is appropriate.

    Once you have an Optional, you can use one of the or methods to get the value the Optional contains (if it contains a value) or some other value (if it does not).

    Putting it all together, your simple example would look like:

    T value = Optional.fromNullable(obj).or(defaultValue);
    

    The extra flexibility of Optional comes in if you want to use a Supplier for the default value (so you don’t do the calculation to get it unless necessary) or if you want to chain multiple optional values together to get the first value that is present, for example:

    T value = someOptional.or(someOtherOptional).or(someDefault);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why does Guava's ImmutableList have so many overloaded of() methods? Looking at
Does anyone know how to get IntelliSense to work reliably when working in C/C++
Does anyone use have a good regex library that they like to use? Most
Does anyone know how to transform a enum value to a human readable value?
Browsing through Guava libraries I saw this weird signature on the readLines method of
Does jQuery 1.5.1 support attribute selectors in the closest method? Given the structure below,
Say for example that I have a Runnable called RunnableA that does something. I
Does the Windows API provide a way to notify a running Delphi application in
Does Windows Phone 7 have anything like iPhone's UITableView? [Basically, I'm porting an iOS
No, this is not asking how to get guava working in GWT, as I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.