crwdns149843:0crwdne149843:0

crwdns149845:0crwdne149845:0

crwdns149847:0crwdne149847:0

crwdns149849:0crwdne149849:0

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

crwdns149851:0crwdne149851:0

crwdns149853:0crwdne149853:0

crwdns149855:0crwdne149855:0

crwdns149857:0crwdne149857:0

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

crwdns149859:0crwdne149859:0

crwdns149861:0crwdne149861:0

crwdns149863:0crwdne149863:0

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

crwdns149865:0crwdne149865:0

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

crwdns149867:0crwdne149867:0

crwdns149863:0crwdne149863:0

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

crwdns149865:0crwdne149865:0

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

crwdns149869:0crwdne149869:0

crwdns149871:0crwdne149871:0

crwdns149863:0crwdne149863:0

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

crwdns149865:0crwdne149865:0

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

Note

crwdns149873:0crwdne149873:0

crwdns149875:0crwdne149875:0

crwdns149877:0crwdne149877:0

crwdns149879:0crwdne149879:0

crwdns149881:0crwdne149881:0

crwdns149863:0crwdne149863:0

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

crwdns149865:0crwdne149865:0

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

crwdns149883:0crwdne149883:0

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

crwdns149885:0crwdne149885:0

crwdns149887:0crwdne149887:0

crwdns149863:0crwdne149863:0

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

crwdns149865:0crwdne149865:0

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

Note

crwdns149889:0crwdne149889:0

crwdns149891:0crwdne149891:0

crwdns149893:0crwdne149893:0

crwdns149863:0crwdne149863:0

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

crwdns149865:0crwdne149865:0

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

Tip

crwdns149895:0crwdne149895:0

crwdns149897:0crwdne149897:0

crwdns149899:0crwdne149899:0

  • crwdns149901:0crwdne149901:0

  • crwdns149903:0crwdne149903:0

  • crwdns149905:0crwdne149905:0

crwdns149907:0crwdne149907: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);
        }
    }
}

crwdns149909:0crwdne149909:0

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

crwdns149911:0crwdne149911:0

crwdns149913:0crwdne149913:0

crwdns149915:0crwdne149915:0

crwdns149917:0crwdne149917:0

crwdns149919:0crwdne149919:0