Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert following method to java 8?

I have List object and I need to take the first element on the list if it is not null or empty.

I write below code using java and now I want to convert it to Java 8.

    List<DD> container
    A<DD,DI> a;
    if(container!=null || !container.isEmpty()){
       for(DD dd:container)
       {
          a = dd.getPrescription();
          break;
       }
    }

I convert it like this.

 DD detail = container.stream().findFirst().get();

I need to know this is correct?

like image 476
uma Avatar asked Jan 03 '19 03:01

uma


People also ask

Why should you upgrade to Java SE 8?

Keeping your code up to date with the latest versions of languages and libraries is a challenging task. Java SE 8 brings entire new concepts to the language, like lambda expressions, and adds new methods to classes that developers have been using comfortably for years.

How to migrate your code from Java 6 to Java 8?

In this post, we will give you some tips and tricks to migrate your code from Java 6 (or 7) to Java 8. If you are new to Java 8 then do some initial setup to compile and run your projects on JDK 8. Make sure you're compiling with a Java 8 JDK. Pick a section of the codebase to apply them to.

What's new in Java 8?

Java 8 introduced a new way of working with collections of data, through the Streams API. For example, java.util.Iterable has a forEach method that lets you pass in a lambda that represents an operation to run on every element. Find the for loop statements in your project, for example : Use Method Reference rather than a lambda.

What is type conversion in Java with example?

Type conversion in Java with Examples. When you assign value of one data type to another, the two types might not be compatible with each other. If the data types are compatible, then Java will perform the conversion automatically known as Automatic Type Conversion and if not then they need to be casted or converted explicitly.


2 Answers

There is a critical flaw in your current code, i.e.

if(container!=null || !container.isEmpty())

this can still throw a NullPointerException (when container == null), unless the conditional operator is changed to &&. Post which the implementation below would be what I would suggest following.


It's almost correct, in the sense that you need to handle some default value if the conditions are not met :

DD detail = container.stream().findFirst().orElse(null); // or some default value instead of 'null'

If the container itself could be null, use

DD detail = container != null ? 
                container.stream().findFirst().orElse(null) : null;

In the case when you need the prescription from this object, use map as :

container.stream().findFirst().map(DD::getPrescription).orElse(null)
//                               ^^
//                               return type of prescription then

With Java-9, this could have been much simpler as :

A<DD, DI> basePrescription = Stream.ofNullable(container) // Java-9 API
                                   .flatMap(List::stream)
                                   .findFirst()
                                   .map(DD::getPrescription)
                                   .orElse(null);
like image 75
Naman Avatar answered Sep 20 '22 13:09

Naman


This is way easier:

A<DD,DI> a = container.get(0).getPrescription();

While this is a direct translation of your original code, you probably intended something like that:

A<DD,DI> a = container != null && !container.isEmpty()
    ? container.get(0).getPrescription()
    : null;
like image 31
xehpuk Avatar answered Sep 21 '22 13:09

xehpuk