How to get the first element of the stream in Java

2 Answers

0 votes
import java.util.Arrays; 
 
public class MyClass {
    public static void main(String args[]) {
        int arr[] = {18, 2, 3, 49383831, 5, 6};
             
        System.out.println(Arrays.stream(arr) 
                                 .findFirst()); 
    }
}
 
 
 
 
/*
run:
 
OptionalInt[18]
 
*/

 



answered Oct 3, 2019 by avibootz
0 votes
import java.util.*;

public class MyClass {
    public static void main(String args[]) {
        int arr[] = {18, 2, 3, 49383831, 5, 6};

        OptionalInt n = Arrays.stream(arr) 
                              .findFirst();
         
        System.out.println(n.getAsInt()); 
    }
}
 
 
 
 
/*
run:
 
18
 
*/

 



answered Oct 3, 2019 by avibootz

Related questions

...