Is there a detect method from Smalltalk in Java?
•
Java
I search for method detection in Java 8 I used to use it in Smalltalk, but Java doesn't seem to have it Smalltalk's detect method finds the first element based on the result of the expression For example:
Listnumbers.detect[number->number>4]
If there is an element greater than 4, the element is returned
Solution
In Java 8, you can use the stream API:
Collection<T> collection = ... // Optional<T> optionalFirst = collection.stream() .filter(e -> /* some predicate */) .findFirst();
This will find the first element in the collection that satisfies the predicate, if any
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码