crwdns139441:0crwdne139441:0

crwdns139443:0crwdne139443:0

crwdns139445:0crwdne139445:0

crwdns139447:0crwdne139447:0

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

crwdns139449:0crwdne139449:0

crwdns139451:0crwdne139451:0

crwdns139453:0crwdne139453:0

crwdns139455:0crwdne139455:0

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

crwdns139457:0crwdne139457:0

crwdns139459:0crwdne139459:0

crwdns139461:0crwdne139461:0

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

crwdns139463:0crwdne139463:0

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

crwdns139465:0crwdne139465:0

crwdns139461:0crwdne139461:0

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

crwdns139463:0crwdne139463:0

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

crwdns139467:0crwdne139467:0

crwdns139469:0crwdne139469:0

crwdns139461:0crwdne139461:0

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

crwdns139463:0crwdne139463:0

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

Note

crwdns139471:0crwdne139471:0

crwdns139473:0crwdne139473:0

crwdns139475:0crwdne139475:0

crwdns139477:0crwdne139477:0

crwdns139479:0crwdne139479:0

crwdns139461:0crwdne139461:0

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

crwdns139463:0crwdne139463:0

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

crwdns139481:0crwdne139481:0

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

crwdns139483:0crwdne139483:0

crwdns139485:0crwdne139485:0

crwdns139461:0crwdne139461:0

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

crwdns139463:0crwdne139463:0

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

Note

crwdns139487:0crwdne139487:0

crwdns139489:0crwdne139489:0

crwdns139491:0crwdne139491:0

crwdns139461:0crwdne139461:0

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

crwdns139463:0crwdne139463:0

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

Tip

crwdns139493:0crwdne139493:0

crwdns139495:0crwdne139495:0

crwdns139497:0crwdne139497:0

  • crwdns139499:0crwdne139499:0

  • crwdns139501:0crwdne139501:0

  • crwdns139503:0crwdne139503:0

crwdns139505:0crwdne139505: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);
        }
    }
}

crwdns139507:0crwdne139507:0

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

crwdns139509:0crwdne139509:0

crwdns139511:0crwdne139511:0

crwdns139513:0crwdne139513:0

crwdns139515:0crwdne139515:0

crwdns139517:0crwdne139517:0