Example usage for java.util Optional get

List of usage examples for java.util Optional get

Introduction

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

Prototype

public T get() 

Source Link

Document

If a value is present, returns the value, otherwise throws NoSuchElementException .

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) {
    Optional<String> optional1 = Optional.empty();
    Optional<String> optional2 = Optional.of("DEMO");

    System.out.println("optional2.get = " + optional2.get());
    System.out.println("optional1.orElse = " + optional1.orElse("Something else"));
    optional2.ifPresent(System.out::println);
    System.out.println("optional1.isPresent = " + optional1.isPresent());

}

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 {//  ww w  . j  a v a 2  s  . co  m
        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 ww  w.j a  va 2  s . co m*/
        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 {//from  ww w. j  a  v a2  s  .com
        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  w w w . ja v a  2 s. c  o m
        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 {//from w w w .ja  va  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 {// w  w  w .j a  va 2  s . com
        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  w  w w  . j  a  v  a 2  s  .  c om
        System.out.println("no value");
    }

}