Example usage for java.util Optional orElse

List of usage examples for java.util Optional orElse

Introduction

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

Prototype

public T orElse(T other) 

Source Link

Document

If a value is present, returns the value, otherwise returns other .

Usage

From source file:Main.java

public static void main(String[] args) {
    Optional<Integer> o = Optional.empty();
    System.out.println(o.orElse(42));
}

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

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

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   w  w w . j ava 2 s.co m
        System.out.println("No Z's found");
    }
}

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> value = Optional.of("some value");
    System.out.println(value.isPresent());
    System.out.println(value.get());
    String str = null;//w w  w  .ja  v a  2  s  .c o  m
    // 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) {
    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:com.arpnetworking.configuration.jackson.BaseJsonNodeSource.java

/**
 * Find the <code>JsonNode</code> if one exists from a specified root node
 * given a sequence of keys to look-up.//from w w  w  .  j a  va2 s  . c o  m
 *
 * @param node The root <code>JsonNode</code>.
 * @param keys The sequence of keys to search for.
 * @return The <code>Optional</code> <code>JsonNode</code> instance.
 */
protected static Optional<JsonNode> getValue(final Optional<JsonNode> node, final String... keys) {
    JsonNode jsonNode = node.orElse(null);
    for (final String key : keys) {
        if (jsonNode == null) {
            break;
        }
        jsonNode = jsonNode.get(key);
    }
    return Optional.ofNullable(jsonNode);
}

From source file:com.microsoft.azure.hdinsight.spark.jobs.YarnRestUtil.java

private static List<App> getSparkAppFromYarn(@NotNull final IClusterDetail clusterDetail)
        throws IOException, HDIException {
    final HttpEntity entity = getYarnRestEntity(clusterDetail, "cluster/apps");
    Optional<YarnApplicationResponse> allApps = ObjectConvertUtils.convertEntityToObject(entity,
            YarnApplicationResponse.class);
    return allApps.orElse(YarnApplicationResponse.EMPTY).getAllApplication().orElse(App.EMPTY_LIST).stream()
            .filter(app -> app.isLivyJob()).collect(Collectors.toList());
}

From source file:be.i8c.codequality.sonar.plugins.sag.webmethods.flow.FlowLanguage.java

/**
 * This methods returns the configured pattern for flow files.
 * //from ww w.  j av a 2 s .  c o  m
 * @return Pattern for flow files
 */
public static String[] getFlowFilePatterns() {
    Optional<String> suffix = config.get(FlowLanguageProperties.FLOW_FILE_FILTER_KEY);
    return new String[] { suffix.orElse(".*") };
}