Java Stream get any element in stream

Description

Java Stream get any element 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   ww w  .  j av a2  s . c  om
    Comparator<String> byShares = String.CASE_INSENSITIVE_ORDER;
    s = s.sorted(byShares);
    // May or may not return a value
    Optional<String> maybe = s.findAny();
    System.out.println("any:" + maybe.get());

 }
}



PreviousNext

Related