com.annimon.stream.Optional.map()方法的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(1.9k)|赞(0)|评价(0)|浏览(154)

本文整理了Java中com.annimon.stream.Optional.map()方法的一些代码示例,展示了Optional.map()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Optional.map()方法的具体详情如下:
包路径:com.annimon.stream.Optional
类名称:Optional
方法名:map

Optional.map介绍

[英]Invokes the given mapping function on inner value if present.
[中]对内部值(如果存在)调用给定的映射函数。

代码示例

代码示例来源:origin: aNNiMON/Lightweight-Stream-API

@Test
public void testMapAsciiToString() {
  Optional<String> result = Optional.of(65)
      .map(new Function<Integer, String>() {
        @Override
        public String apply(Integer value) {
          return String.valueOf((char) value.intValue());
        }
      });
  assertThat(result, hasValue("A"));
}

代码示例来源:origin: avluis/Hentoid

public static boolean isOnline() {
    return Optional.of(HentoidApp.getAppContext())
        .map(context -> (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE))
        .map(ConnectivityManager::getActiveNetworkInfo)
        .mapToBoolean(netInfo -> netInfo.isAvailable() && netInfo.isConnected())
        .orElse(false);
  }
}

代码示例来源:origin: aNNiMON/Lightweight-Stream-API

@Test
public void testMapOnEmptyOptional() {
  assertFalse(
      Optional.<Integer>empty()
          .map(UnaryOperator.Util.<Integer>identity())
          .isPresent());
}

代码示例来源:origin: GrossumUA/TAS_Android_Boilerplate

@Override
  public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    return chain.proceed(
        preferencesManager.getToken().map(token -> request.newBuilder()
            .header(AUTHORIZATION_KEY, token)
            .method(request.method(), request.body())
            .build()).orElse(request)
    );
  }
}

代码示例来源:origin: GrossumUA/TAS_Android_Boilerplate

public static <T> Observable<T> ofOptional(Optional<T> optional) {
  return optional.map(Observable::just).orElse(Observable.empty());
}

代码示例来源:origin: GrossumUA/TAS_Android_Boilerplate

public static <T> Maybe<T> ofOptionalMaybe(Optional<T> optional) {
  return optional.map(Maybe::just).orElse(Maybe.empty());
}

相关文章