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) {
    List<String> names = Arrays.asList("XML", "HTML", "Java", "Javascript", "CSS", "Static");

    Optional<String> firstZ = names.stream().filter(name -> name.startsWith("Z")).findFirst();
    if (firstZ.isPresent()) {
        System.out.println(firstZ.get());
    } else {//  w  ww  .  j a v  a2 s. c o  m
        System.out.println("No Z's found");
    }
}

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 {// w  w  w .j  a  va 2s  . c o  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.");
    }

}

From source file:Main.java

public static void main(String[] args) {
    Optional<Integer> value = Optional.empty();

    System.out.println(value.orElse(42));
    System.out.println(value.isPresent());
    System.out.println(value.get());
}

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());
    String str = null;/*from w  w w .ja v  a 2  s. c om*/
    // Optional.of(str);

    Optional<Integer> o = Optional.empty();
    System.out.println(o.isPresent());
    System.out.println(o.orElse(42));

    List<Integer> results = new ArrayList<>();
    Optional<Integer> second = Optional.of(3);
    second.ifPresent(results::add); // must operate via side-effects,
                                    // unfortunately...
    System.out.println(results);

    o = Optional.empty();
    System.out.println(o.orElse(42));

    o = Optional.of(42);
    System.out.println(o.get());

    o = Optional.empty();
    o.get();
}

From source file:Main.java

public static void main(String[] args) {
    List<String> names = Arrays.asList("Bob", "Tom", "Jeff", "Scott", "Jennifer", "Steve");

    Optional<String> firstS = names.stream().filter(name -> name.startsWith("S")).findFirst();
    System.out.println(firstS.orElse("None found"));

    Optional<String> firstZ = names.stream().filter(name -> name.startsWith("Z")).findFirst();
    if (firstZ.isPresent()) {
        System.out.println(firstZ.get());
    } else {/*from www  .  j  av  a 2 s. c  om*/
        System.out.println("No Z's found");
    }
}

From source file:Main.java

public static void main(String[] args) {
    List<String> names = Arrays.asList("XML", "HTML", "CSS", "Javascript", "Java", "Static");

    Optional<String> longestName = names.stream()
            .reduce((name1, name2) -> name1.length() >= name2.length() ? name1 : name2);

    if (longestName.isPresent()) {
        System.out.println(longestName.get());
    } else {/*from   w  w  w .  j av a  2s.c o m*/
        System.out.println("WTF?");
    }

}

From source file:Main.java

public static void main(String[] args) {
    List<Employee> persons = Employee.persons();
    // Find any male
    Optional<Employee> anyMale = persons.stream().filter(Employee::isMale).findAny();
    if (anyMale.isPresent()) {
        System.out.println("Any male:   " + anyMale.get());
    } else {/* w  ww. j  a  v  a  2  s . c o  m*/
        System.out.println("No male  found.");
    }
    // Find the first male
    Optional<Employee> firstMale = persons.stream().filter(Employee::isMale).findFirst();
    if (firstMale.isPresent()) {
        System.out.println("First male:   " + anyMale.get());
    } else {
        System.out.println("No male  found.");
    }
}

From source file:io.mapzone.arena.csw.GetRecordByIdRequest.java

/**
 * Test./*from   w w  w . ja  v  a  2 s  .c om*/
 */
public static final void main(String[] args) throws Exception {
    //        GetRecordsRequest getRecords = new GetRecordsRequest()
    //              .constraint.put( "AnyText Like '%Landschaftsschutzgebiete%'" )
    //              .constraint.put( "*Landschaftsschutzgebiete*" )
    //                .baseUrl.put( "http://www.geokatalog-mittelsachsen.de/geonetwork2.10/srv/eng/csw" );

    GetRecordByIdRequest<RecordXML> getRecord = new GetRecordByIdRequest<RecordXML>().identifier
            .put("e2d17537-1cf3-4406-9980-572e41c027d2").baseUrl.put("http://localhost:8090/csw");

    Optional<RecordXML> record = getRecord.execute(new NullProgressMonitor());
    System.out.println("-----------------------------------");
    System.out.println(record.get().identifier);
}

From source file:Main.java

public static void main(String[] args) {
    Optional<Employee> person = Employee.persons().stream()
            .max(Comparator.comparingDouble(Employee::getIncome));

    if (person.isPresent()) {
        System.out.println("Highest earner: " + person.get());
    } else {//from  w w w  . j a  v a  2s.  c  o  m
        System.out.println("Could not  get   the   highest earner.");
    }
}

From source file:Main.java

public static void main(String[] args) {
    Optional<Employee> person = Employee.persons().stream()
            .reduce((p1, p2) -> p1.getIncome() > p2.getIncome() ? p1 : p2);
    if (person.isPresent()) {
        System.out.println("Highest earner: " + person.get());
    } else {//from  w  ww. j a  va 2  s. co  m
        System.out.println("Could not  get   the   highest earner.");
    }
}