Like C# LINQ Enumerable#First with delegate method and Scala List#find method, In Java8, is there a method that finds first matched element with a condition in list?
Asked
Active
Viewed 2,320 times
2
-
Related: find all: http://stackoverflow.com/questions/122105/what-is-the-best-way-to-filter-a-java-collection – Ciro Santilli OurBigBook.com Mar 23 '15 at 16:45
2 Answers
6
Yes, use Stream#findFirst()
method:
List<String> list = Arrays.asList("a", "ab", "bc", "abc");
String firstMatch = list.stream().filter(s -> s.length() == 2).findFirst().get();

Rohit Jain
- 209,639
- 45
- 409
- 525
-
Thank you for your rapid answer and nice sample codes. And I'm sorry. My Quesition is so bad. I knew it. I want to know that Is there high order function to find element that matched condition. Like C# Enumerable#First and Scala List#find with delegate or closure – RyotaMurohoshi Apr 09 '14 at 19:15
-
@muhron Can you clarify what you mean by high order function, if not what is being given in the answer already? – Rohit Jain Apr 09 '14 at 19:16
-
Sorry my comment is so bad. Yes, you are right. Filter method accepts a funcition I provide. The way you told me is two methods chain, filter and findFirst. I want to know existing the way that add lambda and collect first matched element with one method. – RyotaMurohoshi Apr 09 '14 at 19:27
-
@muhron No there is no way to do this with one method. You might be able to create your own. But Java doesn't provide one. – Rohit Jain Apr 09 '14 at 19:36
-
Thank you. I'm happy because I have just learned the method doesn't exist. I wanted to know that does it exist or not. So, different from C#, scala and groovy, In java-8 we should use two method combinations? Is it true? It is calture and API designing, ok? – RyotaMurohoshi Apr 09 '14 at 19:42
-
2@muhron Why do you insist on having a single method? The point is, a single method should do only a single thing. You can club the `filter` and `findFirst` methods into one, but then that will not be a very moduler design. Reusing the methods will not be possible. APIs are designed taking such things into consideration. Of course, they can't be pre-cooked according to the user's need. – Rohit Jain Apr 09 '14 at 19:45
-
With C# I use Enumerable#First with delegate many times. And I want to know is exist like that method and how to write in Java8, single method or methods combination. But now, from you and other person comments, I understand that java API designing is prefer a minimal API and they are combined. So I understand that I should use two method combination in Java8. I know that the word : When in Rome, do as the Romans do. So I'll write that process with filter and findFirst combinations. Thank you for your nice comments. – RyotaMurohoshi Apr 09 '14 at 20:15
6
list.stream().filter(predicate).findFirst();

Louis Wasserman
- 191,574
- 25
- 345
- 413
-
Thank you for your rapid answer. And I'm sorry. My Quesition is so bad. I knew it. I want to know that Is there high order function to find element that matched condition. Like C# Enumerable#First and Scala List#find with delegate or closure – RyotaMurohoshi Apr 09 '14 at 19:14
-
...I don't understand what about that question is not answered by this answer. `filter` accepts a function you provide; it's higher-order. – Louis Wasserman Apr 09 '14 at 19:15
-
Sorry my comment is so bad. Yes, you are right. Filter method accepts a funcition I provide. The way you told me is two methods chain, filter and findFirst. I want to know existing the way that add lambda and collect first matched element with one method. – RyotaMurohoshi Apr 09 '14 at 19:30
-
2Why does it matter that it's two methods instead of one? The whole point of the API is to provide a few building blocks that you can stick together to do whatever you want; it'd be ridiculous to try to provide one method for each possible combination of operations. – Louis Wasserman Apr 09 '14 at 19:31
-
I'm happy if I know that there is no way with one method. I want to know that does it exist or not. So, In java-8, should we use two method combinations? Is it java-8 calture or API designing? – RyotaMurohoshi Apr 09 '14 at 19:38
-
2There is no way with one method, which is a result of a deliberate tendency to prefer a minimal API that can still be combined to accomplish everything it needs to. – Louis Wasserman Apr 09 '14 at 19:43
-
Oh, I understand! > _result of a deliberate tendency to prefer a minimal API that can still be combined to accomplish everything it needs to._ Thank you so much. I can understand the reason why there is no method like Enumerable#First[C#] in java from your comment. Thenk you your nice comments! – RyotaMurohoshi Apr 09 '14 at 20:20