crwdns119827:0crwdne119827:0

crwdns119829:0crwdne119829:0

crwdns119831:0crwdne119831:0

crwdns119833:0crwdne119833:0

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

crwdns119835:0crwdne119835:0

crwdns119837:0crwdne119837:0

crwdns119839:0crwdne119839:0

crwdns119841:0crwdne119841:0

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

crwdns119843:0crwdne119843:0

crwdns119845:0crwdne119845:0

crwdns119847:0crwdne119847:0

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

crwdns119849:0crwdne119849:0

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

crwdns119851:0crwdne119851:0

crwdns119847:0crwdne119847:0

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

crwdns119849:0crwdne119849:0

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

crwdns119853:0crwdne119853:0

crwdns119855:0crwdne119855:0

crwdns119847:0crwdne119847:0

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

crwdns119849:0crwdne119849:0

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

Note

crwdns119857:0crwdne119857:0

crwdns119859:0crwdne119859:0

crwdns119861:0crwdne119861:0

crwdns119863:0crwdne119863:0

crwdns119865:0crwdne119865:0

crwdns119847:0crwdne119847:0

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

crwdns119849:0crwdne119849:0

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

crwdns119867:0crwdne119867:0

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

crwdns119869:0crwdne119869:0

crwdns119871:0crwdne119871:0

crwdns119847:0crwdne119847:0

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

crwdns119849:0crwdne119849:0

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

Note

crwdns119873:0crwdne119873:0

crwdns119875:0crwdne119875:0

crwdns119877:0crwdne119877:0

crwdns119847:0crwdne119847:0

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

crwdns119849:0crwdne119849:0

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

Tip

crwdns119879:0crwdne119879:0

crwdns119881:0crwdne119881:0

crwdns119883:0crwdne119883:0

  • crwdns119885:0crwdne119885:0

  • crwdns119887:0crwdne119887:0

  • crwdns119889:0crwdne119889:0

crwdns119891:0crwdne119891: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);
        }
    }
}

crwdns119893:0crwdne119893:0

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

crwdns119895:0crwdne119895:0

crwdns119897:0crwdne119897:0

crwdns119899:0crwdne119899:0

crwdns119901:0crwdne119901:0

crwdns119903:0crwdne119903:0