Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert below method to Java 8 inline function?

I need to convert below method java 8 inline function. need some expert help and explanation to do this.

@Override
public boolean a(final Collection<DoseDetailMutableDTO> detailModels) {
    for (DoseDetailMutableDTO dd : detailModels) {
         final boolean doseDetailTextScheduled = isDoseDetailTextScheduled(dd, 1);
         if (doseDetailTextScheduled) {
             return true;
         }
    }
    return false;
}

and Are there any short cut to do this intelj IDE ?

like image 811
uma Avatar asked Dec 21 '18 05:12

uma


People also ask

How do you write an inline function in Java?

No, there is no inline function in java. Yes, you can use a public static method anywhere in the code when placed in a public class. The java compiler may do inline expansion on a static or final method, but that is not guaranteed.

Is there inline function in Java?

No, Java does not provide inline functions it is typically done by the JVM at execution time.

What are the two types of streams offered by Java 8?

Java 8 offers the possibility to create streams out of three primitive types: int, long and double. As Stream<T> is a generic interface, and there is no way to use primitives as a type parameter with generics, three new special interfaces were created: IntStream, LongStream, DoubleStream.

What is Stream pipeline in Java 8?

Introduced in Java 8, the Stream API is used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.


Video Answer


3 Answers

You can make use of Stream.anyMatch as:

public boolean a(final Collection<DoseDetailMutableDTO> detailModels) {
    return detailModels.stream()
                       .anyMatch(dd -> isDoseDetailTextScheduled(dd, 1));
}

returns true if any elements of the stream match the provided predicate, otherwise false

Edit: (from comments)

The control to learn for such suggested shortcuts on IntelliJ IDEA is Ctrl+Space or on MacOS can use Alt+Enter as well.

like image 63
Naman Avatar answered Oct 17 '22 18:10

Naman


We can try using a stream here:

@Override
public boolean a (final Collection<DoseDetailMutableDTO> detailModels) {
    return detailModels.stream()
               .filter(x -> isDoseDetailTextScheduled(x, 1))
               .findFirst()
               .orElse(false);
}

Actually, to make your method null safe, in the event that the input list might be null, we can try this:

@Override
public boolean a (final Collection<DoseDetailMutableDTO> detailModels) {
    return Optional.ofNullable(detailModels)
                   .map(Collection::stream)
                   .orElseGet(Stream::empty)
                   .filter(x -> isDoseDetailTextScheduled(x, 1))
                   .findFirst()
                   .orElse(false);
}
like image 39
Tim Biegeleisen Avatar answered Oct 17 '22 17:10

Tim Biegeleisen


You can use anyMatch for this. Since the second parameter to the function is constant you can write a method that calls isDoseDetailTextScheduled. I think it becomes even more concise:

public boolean a(final Collection<DoseDetailMutableDTO> detailModels) {
   return detailModels.stream().anyMatch(this::isDoseDetailTextScheduledOne);
}

public boolean isDoseDetailTextScheduledOne(DoseDetailMutableDTO dto) {
    return isDoseDetailTextScheduled(dto, 1);
}
like image 2
fastcodejava Avatar answered Oct 17 '22 19:10

fastcodejava