Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA 8 Stream filter using Predicate to fetch latest record

I have a method of type boolean which returns stream of data of Predicate type Incident, I want to filter out the latest incident only.

like image 491
not-a-bug Avatar asked May 25 '20 11:05

not-a-bug


People also ask

How can I get the last element of a stream?

Getting the Last Element of an Infinite StreamStream<Integer> stream = Stream. iterate(0, i -> i + 1); stream. reduce((first, second) -> second). orElse(null);

What is the purpose of filter () method in streams?

The filter() function of the Java stream allows you to narrow down the stream's items based on a criterion. If you only want items that are even on your list, you can use the filter method to do this. This method accepts a predicate as an input and returns a list of elements that are the results of that predicate.

What is the purpose of filter method of stream in Java 8?

Java 8 Stream interface introduces filter() method which can be used to filter out some elements from object collection based on a particular condition. This condition should be specified as a predicate which the filter() method accepts as an argument.

How to filter stream elements based on predicate in Java?

Java stream provides a method filter () to filter stream elements on the basis of given predicate. Suppose you want to get only even elements of your list then you can do this easily with the help of filter method. This method takes predicate as an argument and returns a stream of consisting of resulted elements.

How do you filter a stream in Java?

Java Stream Filter. Java stream provides a method filter() to filter stream elements on the basis of given predicate. Suppose you want to get only even elements of your list then you can do this easily with the help of filter method. This method takes predicate as an argument and returns a stream of consisting of resulted elements.

How do I pass a second predicate to the filter() method?

Pass the second predicate as a parameter to the and () function on the first predicate. This signifies that the first predicate receives each instance from the stream. If the first predicate returns true, the second predicate receives the same value. Finally, the result of the filter () method will be satisfied by the first and second predicate's.

What is predicate in streams?

Predicate is passed as an argument to the filter () method. Each value in the stream is evaluated to this predicate logic. There are only a few methods in Predicate functional interface, such as and (), or (), or negate (), and isEquals ().


2 Answers

You have to check all elements in stream. You can collect all filtered elements to LinkedList and get the last one.

forloebs.stream()
.filter(predicate)
.collect(Collectors.toCollection(LinkedList::new)).peekLast();

peekLast will return null if this list is empty.

like image 74
lczapski Avatar answered Oct 24 '22 07:10

lczapski


You can use the max function to get latest incident that matches your completeincident condition.

Optional<Incident> latestIncident = forloebs.stream()
 .filter(completeincident)
 .max(Comparator.comparing(x -> x.getDateIncident()));

The comparator will keep the latest element based on getDateIncident.

It will return Optional.Empty if none of them matches filter criteria.

like image 43
lucid Avatar answered Oct 24 '22 07:10

lucid