crwdns130579:0crwdne130579:0

crwdns130581:0crwdne130581:0

crwdns130583:0crwdne130583:0

crwdns130585:0crwdne130585:0

Optional<String> opt = getOptionalString();
String wrappedString = opt.get();

crwdns130587:0crwdne130587:0

crwdns130589:0crwdne130589:0

crwdns130591:0crwdne130591:0

crwdns130593:0crwdne130593:0

Optional<String> opt = getOptionalString();
if (opt.isPresent()) {
    String wrappedString = opt.get();
    // more code
}

crwdns130595:0crwdne130595:0

crwdns130597:0crwdne130597:0

crwdns130599:0crwdne130599:0

Optional<String> optionalString = optionalString();
String someString;
if (optionalString.isPresent()) {
    someString = optionalString.get();
} else {
    someString = DEFAULT_STRING;
}

crwdns130601:0crwdne130601:0

String someString = getOptionalString().orElse(DEFAULT_STRING);

crwdns130603:0crwdne130603:0

crwdns130599:0crwdne130599:0

Optional<String> optionalString = optionalString();
String someString;
if (optionalString.isPresent()) {
    someString = optionalString.get();
} else {
    someString = myPlugin.defaultString();
}

crwdns130601:0crwdne130601:0

String someString = getOptionalString().orElseGet(myPlugin::defaultString);

crwdns130605:0crwdne130605:0

crwdns130607:0crwdne130607:0

crwdns130599:0crwdne130599:0

Optional<String> optionalString = optionalString();
if (!optionalString.isPresent()) {
    throw new MyException();
}
String someString = optionalString.get();

crwdns130601:0crwdne130601:0

String someString = getOptionalString().orElseThrow(MyException::new);

Note

crwdns130609:0crwdne130609:0

crwdns130611:0crwdne130611:0

crwdns130613:0crwdne130613:0

crwdns130615:0crwdne130615:0

crwdns130617:0crwdne130617:0

crwdns130599:0crwdne130599:0

Optional<String> optionalString = getOptionalString();
if (optionalString.isPresent()) {
    myPlugin.doSomethingWithString(optionalString.get());
}

crwdns130601:0crwdne130601:0

Optional<String> optionalString = getOptionalString();
optionalString.ifPresent(s -> myPlugin.doSomethingWithString(s));

crwdns130619:0crwdne130619:0

getOptionalString().ifPresent(myPlugin::doSomethingWithString);

crwdns130621:0crwdne130621:0

crwdns130623:0crwdne130623:0

crwdns130599:0crwdne130599:0

Optional<String> optionalString = getOptionalString();
if (optionalString.isPresent()) {
    String someString = optionalString.get();
    if (stringTester.isPalindromic(someString)) {
        myPlugin.doSomethingWithString(someString);
    }
}

crwdns130601:0crwdne130601:0

getOptionalString()
      .filter(stringTester::isPalindromic)
      .ifPresent(myPlugin::doSomethingWithString);

Note

crwdns130625:0crwdne130625:0

crwdns130627:0crwdne130627:0

crwdns130629:0crwdne130629:0

crwdns130599:0crwdne130599:0

Optional<String> optionalString = getOptionalString();
if (optionalString.isPresent()) {
    String someString = optionalString.toLowerCase();
    myPlugin.doSomethingWithString(someString);
}

crwdns130601:0crwdne130601:0

getOptionalString()
      .map(s -> s.toLowerCase())
      .ifPresent(myPlugin::doSomethingWithString);

Tip

crwdns130631:0crwdne130631:0

crwdns130633:0crwdne130633:0

crwdns130635:0crwdne130635:0

  • crwdns130637:0crwdne130637:0

  • crwdns130639:0crwdne130639:0

  • crwdns130641:0crwdne130641:0

crwdns130643:0crwdne130643:0

if (optionalPlayer.isPresent()) {
    Player player = optionalPlayer.get();
    Optional<Pet> optionalPet = petRegistry.getPetForPlayer(player);
    if (optionalPet.isPresent()) {
        Pet pet = optionalPet.get();
        if (petHelper.canSpawn(pet)) {
            petHelper.spawnPet(pet);
        }
    }
}

crwdns130645:0crwdne130645:0

optionalPlayer
      .flatMap(petRegistry::getPetForPlayer)
      .filter(petHelper::canSpawn)
      .ifPresent(petHelper::spawnPet);

crwdns130647:0crwdne130647:0

crwdns130649:0crwdne130649:0

crwdns130651:0crwdne130651:0

crwdns130653:0crwdne130653:0

crwdns130655:0crwdne130655:0