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

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

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

Optional.of介绍

[英]Returns an Optional with the specified present non-null value.
[中]

代码示例

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

@Override
  public Optional<Integer> get() {
    return Optional.of(19);
  }
}).get();

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

@Test
public void testEqualsWithDifferentNullableState() {
  final Optional<Integer> optInt = Optional.of(10);
  final Optional<Integer> optIntNullable = Optional.ofNullable(10);
  assertEquals(optInt, optIntNullable);
}

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

@Test
public void testEqualsWithDifferentGenericTypes() {
  final Optional<Student> s1 = Optional.of(student);
  final Optional<Integer> optInt = Optional.of(10);
  assertNotEquals(s1, optInt);
}

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

@Test
public void testHashCodeWithDifferentNullableState() {
  final Optional<Integer> optInt = Optional.of(10);
  final Optional<Integer> optIntNullable = Optional.ofNullable(10);
  assertEquals(optInt.hashCode(), optIntNullable.hashCode());
}

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

@Test
public void testEqualsTransitive() {
  final Optional<Student> s1 = Optional.of(student);
  final Optional<Student> s2 = Optional.of(student);
  final Optional<Student> s3 = Optional.of(student);
  assertEquals(s1, s2);
  assertEquals(s2, s3);
  assertEquals(s1, s3);
}

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

@Test
public void testExecuteIfAbsentOnPresentValue() {
  Optional.of(10)
      .executeIfAbsent(new Runnable() {
        @Override
        public void run() {
          fail();
        }
      });
}

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

@Test
public void testHashCodeWithDifferentGenericType() {
  final Optional<Student> s1 = Optional.of(student);
  final Optional<Integer> optInt = Optional.of(10);
  assertNotEquals(s1.hashCode(), optInt.hashCode());
}

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

@Test
public void testIfPresentOrElseWhenValuePresentAndEmptyActionNull() {
  Optional.of(10).ifPresentOrElse(new Consumer<Integer>() {
    @Override
    public void accept(Integer value) {
      assertEquals(10, (int) value);
    }
  }, null);
}

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

@Test(expected = NullPointerException.class)
public void testFlatMapWithNullResultFunction() {
  Optional.of(10)
      .flatMap(new Function<Integer, Optional<String>>() {
        @Override
        public Optional<String> apply(Integer value) {
          return null;
        }
      });
}

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

@Test
public void testStream() {
  long count = Optional.of(10).stream().count();
  assertThat(count, is(1L));
}

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

@Test
public void testOrElseThrowWithPresentValue() {
  int value = Optional.of(10).orElseThrow();
  assertEquals(10, value);
}

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

@Test
public void testGetWithPresentValue() {
  int value = Optional.of(10).get();
  assertEquals(10, value);
}

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

@Test
public void testEqualsWithDifferentTypes() {
  final Optional<Integer> optInt = Optional.of(10);
  assertFalse(optInt.equals(10));
}

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

@Test
  public void testToStringWithPresentValue() {
    assertEquals("Optional[10]", Optional.of(10).toString());
  }
}

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

@Test
public void testSelectInvalidSubclassOnOptional() {
  Number number = 42;
  Optional<String> result = Optional.of(number)
      .select(String.class);
  assertThat(result, isEmpty());
}

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

@Test
public void testOr() {
  int value = Optional.of(42).or(new Supplier<Optional<Integer>>() {
    @Override
    public Optional<Integer> get() {
      return Optional.of(19);
    }
  }).get();
  assertEquals(42, value);
}

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

@Test
public void testHasValueThat() {
  Optional<String> optional = Optional.of("text");
  assertThat(optional, hasValueThat(startsWith("te")));
  assertThat(hasValueThat(is(42)), description(is("Optional value is <42>")));
}

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

@Test
public void testHasValue() {
  Optional<String> optional = Optional.of("text");
  assertThat(optional, hasValue("text"));
  assertThat(optional, not(hasValue("test")));
  assertThat(hasValue(42), description(is("Optional value is <42>")));
}

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

@Test
public void testFilter() {
  Optional<Integer> result = Optional.of(10)
      .filter(Predicate.Util.negate(Functions.remainder(2)));
  assertThat(result, isEmpty());
}

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

@Test
public void testSelectValidSubclassOnOptional() {
  Number number = 42;
  Optional<Integer> result = Optional.of(number)
      .select(Integer.class);
  assertThat(result, isPresent());
  assertThat(result, hasValue(42));
}

相关文章