當前位置: 首頁>>代碼示例>>Java>>正文


Java OptionalDouble.ifPresent方法代碼示例

本文整理匯總了Java中java.util.OptionalDouble.ifPresent方法的典型用法代碼示例。如果您正苦於以下問題:Java OptionalDouble.ifPresent方法的具體用法?Java OptionalDouble.ifPresent怎麽用?Java OptionalDouble.ifPresent使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.util.OptionalDouble的用法示例。


在下文中一共展示了OptionalDouble.ifPresent方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: usingJava8

import java.util.OptionalDouble; //導入方法依賴的package包/類
public void usingJava8() {
    // Using Java 8 techniques to find mean
    OptionalDouble mean = Arrays.stream(testData).average();
    if (mean.isPresent()) {
        out.println("The mean is " + mean.getAsDouble());
    } else {
        out.println("The stream was empty");
    }

    mean = Arrays.stream(testData).average();
    mean.ifPresent(x -> out.println("The mean is " + x));

    mean = Arrays.stream(testData).average();
    out.println("The mean is " + mean.orElse(0));

}
 
開發者ID:PacktPublishing,項目名稱:Java-Data-Science-Made-Easy,代碼行數:17,代碼來源:Main.java

示例2: testEmpty

import java.util.OptionalDouble; //導入方法依賴的package包/類
@Test(groups = "unit")
public void testEmpty() {
    OptionalDouble empty = OptionalDouble.empty();
    OptionalDouble present = OptionalDouble.of(1.0);

    // empty
    assertTrue(empty.equals(empty));
    assertTrue(empty.equals(OptionalDouble.empty()));
    assertTrue(!empty.equals(present));
    assertTrue(0 == empty.hashCode());
    assertTrue(!empty.toString().isEmpty());
    assertTrue(!empty.isPresent());
    empty.ifPresent(v -> { fail(); });
    assertEquals(2.0, empty.orElse(2.0));
    assertEquals(2.0, empty.orElseGet(()-> 2.0));
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:17,代碼來源:BasicDouble.java

示例3: testPresent

import java.util.OptionalDouble; //導入方法依賴的package包/類
@Test(groups = "unit")
    public void testPresent() {
    OptionalDouble empty = OptionalDouble.empty();
    OptionalDouble present = OptionalDouble.of(1.0);

    // present
    assertTrue(present.equals(present));
    assertFalse(present.equals(OptionalDouble.of(0.0)));
    assertTrue(present.equals(OptionalDouble.of(1.0)));
    assertTrue(!present.equals(empty));
    assertTrue(Double.hashCode(1.0) == present.hashCode());
    assertFalse(present.toString().isEmpty());
    assertTrue(-1 != present.toString().indexOf(Double.toString(present.getAsDouble()).toString()));
    assertEquals(1.0, present.getAsDouble());
    try {
        present.ifPresent(v -> { throw new ObscureException(); });
        fail();
    } catch(ObscureException expected) {

    }
    assertEquals(1.0, present.orElse(2.0));
    assertEquals(1.0, present.orElseGet(null));
    assertEquals(1.0, present.orElseGet(()-> 2.0));
    assertEquals(1.0, present.orElseGet(()-> 3.0));
    assertEquals(1.0, present.<RuntimeException>orElseThrow(null));
    assertEquals(1.0, present.<RuntimeException>orElseThrow(ObscureException::new));
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:28,代碼來源:BasicDouble.java

示例4: testEmpty

import java.util.OptionalDouble; //導入方法依賴的package包/類
@Test(groups = "unit")
public void testEmpty() {
    OptionalDouble empty = OptionalDouble.empty();
    OptionalDouble present = OptionalDouble.of(1.0);

    // empty
    assertTrue(empty.equals(empty));
    assertTrue(empty.equals(OptionalDouble.empty()));
    assertTrue(!empty.equals(present));
    assertTrue(0 == empty.hashCode());
    assertTrue(!empty.toString().isEmpty());
    assertTrue(!empty.isPresent());

    empty.ifPresent(v -> { fail(); });

    AtomicBoolean emptyCheck = new AtomicBoolean();
    empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true));
    assertTrue(emptyCheck.get());

    try {
        empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); });
        fail();
    } catch (ObscureException expected) {
    } catch (AssertionError e) {
        throw e;
    } catch (Throwable t) {
        fail();
    }

    assertEquals(2.0, empty.orElse(2.0));
    assertEquals(2.0, empty.orElseGet(()-> 2.0));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:33,代碼來源:BasicDouble.java


注:本文中的java.util.OptionalDouble.ifPresent方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。