Java Stream get first in stream

Description

Java Stream get first in stream



import java.util.Comparator;
import java.util.Optional;
import java.util.stream.Stream;

public class Main {

  public static void main(String[] args) {
    Stream<String> s = Stream.of("Groovy","Scala","Ruby","Java","CSS","Kotlin");
    //from w  w  w  . jav a2s . c o  m
    Comparator<String> byShares = String.CASE_INSENSITIVE_ORDER;
    s = s.sorted(byShares);
    // May or may not return a value
    Optional<String> maybe = s.findFirst();
    System.out.println("First:" + maybe.get());

 }
}



PreviousNext

Related