Example usage for java.util Optional isPresent

List of usage examples for java.util Optional isPresent

Introduction

In this page you can find the example usage for java.util Optional isPresent.

Prototype

public boolean isPresent() 

Source Link

Document

If a value is present, returns true , otherwise false .

Usage

From source file:Main.java

public static void main(String[] args) {
    Optional<String> optional = Optional.of("bam");

    optional.isPresent(); // true
    optional.get(); // "bam"
    optional.orElse("fallback"); // "bam"

    optional.ifPresent((s) -> System.out.println(s.charAt(0))); // "b"
}

From source file:Main.java

public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    Optional<Integer> o = numbers.stream().findAny();
    if (o.isPresent()) {
        System.out.println(o.get());
    } else {//  w  w  w .  j  ava2 s.  com
        System.out.println("no value");
    }

}

From source file:Main.java

public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    Optional<Integer> o = numbers.stream().findFirst();
    if (o.isPresent()) {
        System.out.println(o.get());
    } else {/*from  w  w w.j a v a2s  .  com*/
        System.out.println("no value");
    }

}

From source file:Main.java

public static void main(String[] args) {
    Optional<String> value = Optional.of("some value");
    System.out.println(value.isPresent());
    System.out.println(value.get());
}

From source file:Main.java

public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    Optional<Integer> sum = numbers.stream().reduce(Integer::sum);
    if (sum.isPresent()) {
        System.out.println(sum.get());
    } else {/* ww w . j  a v a 2s  . c o  m*/
        System.out.println("noValue ");
    }

}

From source file:Main.java

public static void main(String[] args) {
    List<String> stringList = Arrays.asList("2", "1", "3", "4");

    Optional<String> m = stringList.stream().min(Comparator.reverseOrder());
    if (m.isPresent()) {
        System.out.println(m.get());
    } else {//from   www.jav a2 s.com
        System.out.println("No Value");
    }

}

From source file:Main.java

public static void main(String[] args) {
    List<String> stringList = Arrays.asList("2", "1", "3", "4");

    Optional<String> m = stringList.stream().max(Comparator.reverseOrder());
    if (m.isPresent()) {
        System.out.println(m.get());
    } else {/* w w w . j  av  a  2 s  . c  o  m*/
        System.out.println("No Value");
    }

}

From source file:Main.java

public static void main(String[] args) {
    Stream<String> s = Stream.of("1", "2", "3");

    Optional<String> o = s.collect(Collectors.maxBy(Comparator.reverseOrder()));

    if (o.isPresent()) {
        System.out.println(o.get());
    } else {//from  ww  w.  j a v a2  s.  c o  m
        System.out.println("no value");
    }

}

From source file:Main.java

public static void main(String[] args) {
    Stream<String> s = Stream.of("1", "2", "3");

    Optional<String> o = s.collect(Collectors.minBy(Comparator.reverseOrder()));

    if (o.isPresent()) {
        System.out.println(o.get());
    } else {//from  www  . j av  a2 s .c om
        System.out.println("no value");
    }

}

From source file:Main.java

public static void main(String[] args) {
    Optional<Integer> max = Stream.of(1, 2, 3, 4, 5).reduce(Integer::max);

    if (max.isPresent()) {
        System.out.println("max = " + max.get());
    } else {/*from   www.  j a  v a2s  .co m*/
        System.out.println("max is not  defined.");
    }

    max = Stream.<Integer>empty().reduce(Integer::max);
    if (max.isPresent()) {
        System.out.println("max = " + max.get());
    } else {
        System.out.println("max is not  defined.");
    }

}