Solved

Is there any advantages using Optional rather than checking list is null or empty

Posted on 2016-09-27
2
19 Views
Last Modified: 2016-09-30
Hi Experts,

Optional.ofNullable(list).isPresent()

if(!list.isEmpty() && list!=null)

what is the difference b/w the above two statements?
what is the advantage use of Optional
0
Comment
Question by:srikotesh
2 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 450 total points
Comment Utility
The purpose of Optional is to help design better APIs in which — just by reading the signature of a method — users can tell whether to expect an optional value. In addition, Optional forces you to actively unwrap an Optional to deal with the absence of a value; as a result, you protect your code against unintended null pointer exceptions.

Nice article: Using Optional Effectively in Java 8
0
 
LVL 15

Assisted Solution

by:gurpsbassi
gurpsbassi earned 50 total points
Comment Utility
what is the difference b/w the above two statements?

One of them doesn't check emptiness !
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now