当前位置: 首页>>代码示例>>Java>>正文


Java Optional.ofNullable方法代码示例

本文整理汇总了Java中java8.util.Optional.ofNullable方法的典型用法代码示例。如果您正苦于以下问题:Java Optional.ofNullable方法的具体用法?Java Optional.ofNullable怎么用?Java Optional.ofNullable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java8.util.Optional的用法示例。


在下文中一共展示了Optional.ofNullable方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testOfNullable

import java8.util.Optional; //导入方法依赖的package包/类
@Test(groups = "unit")
public void testOfNullable() {
    Optional<String> instance = Optional.ofNullable(null);
    assertFalse(instance.isPresent());

    instance = Optional.ofNullable("Duke");
    assertTrue(instance.isPresent());
    assertEquals(instance.get(), "Duke");
}
 
开发者ID:streamsupport,项目名称:streamsupport,代码行数:10,代码来源:Basic.java

示例2: getValue

import java8.util.Optional; //导入方法依赖的package包/类
/**
 * Returns a property.
 *
 * @param <T> property type
 * @param key key
 * @return the property; an empty {@link java.util.Optional} will be
 * returned if the property does not exist or the type does not match
 */
public <T> Optional<T> getValue(String key) {

    Object value = map.get(key);

    try {
        return Optional.ofNullable((T) value);
    } catch (ClassCastException ex) {
        return Optional.empty();
    }
}
 
开发者ID:lyrachord,项目名称:FX3DAndroid,代码行数:19,代码来源:PropertyStorage.java

示例3: reducing

import java8.util.Optional; //导入方法依赖的package包/类
/**
 * Returns a {@code Collector} which performs a reduction of its
 * input elements under a specified {@code BinaryOperator}.  The result
 * is described as an {@code Optional<T>}.
 *
 * <p><b>API Note:</b><br>
 * The {@code reducing()} collectors are most useful when used in a
 * multi-level reduction, downstream of {@code groupingBy} or
 * {@code partitioningBy}.  To perform a simple reduction on a stream,
 * use {@link Stream#reduce(BinaryOperator)} instead.
 *
 * <p>For example, given a stream of {@code Person}, to calculate tallest
 * person in each city:
 * <pre>{@code
 *     Comparator<Person> byHeight = Comparator.comparing(Person::getHeight);
 *     Map<City, Optional<Person>> tallestByCity
 *         = people.stream().collect(groupingBy(Person::getCity, reducing(BinaryOperator.maxBy(byHeight))));
 * }</pre>
 *
 * @param <T> element type for the input and output of the reduction
 * @param op a {@code BinaryOperator<T>} used to reduce the input elements
 * @return a {@code Collector} which implements the reduction operation
 *
 * @see #reducing(Object, BinaryOperator)
 * @see #reducing(Object, Function, BinaryOperator)
 */
public static <T> Collector<T, ?, Optional<T>>
reducing(BinaryOperator<T> op) {
    class OptionalBox implements Consumer<T> {
        T value = null;
        boolean present = false;

        @Override
        public void accept(T t) {
            if (present) {
                value = op.apply(value, t);
            }
            else {
                value = t;
                present = true;
            }
        }
    }

    return new CollectorImpl<T, OptionalBox, Optional<T>>(
            OptionalBox::new, OptionalBox::accept,
            (a, b) -> { if (b.present) a.accept(b.value); return a; },
            a -> Optional.ofNullable(a.value), CH_NOID);
}
 
开发者ID:streamsupport,项目名称:streamsupport,代码行数:50,代码来源:Collectors.java


注:本文中的java8.util.Optional.ofNullable方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。