Streams – findFirst() operation
Optional<T> findFirst() class Test { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(Arrays.asList(3, 7, 9, 12)); Optional<Integer> resultOptional = numbers.stream().findFirst(); if (resultOptional.isPresent()) { // checking if Optional contains any value System.out.println(“The first element in the stream is: ” + resultOptional.get()); // getting the value from Optional } else { System.out.println(“The stream is…
Read More Streams – findFirst() operation