You could use a filter:

Optional.ofNullable(s).filter(not(String::isEmpty));

That will return an empty Optional if ppo is null or empty.

Answer from assylias on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › core java › guide to java optional
Guide To Java Optional | Baeldung
February 15, 2026 - Let’s now look at how the above code could be refactored in Java 8. In typical functional programming style, we can execute perform an action on an object that is actually present: @Test public void givenOptional_whenIfPresentWorks_thenCorrect() { Optional<String> opt = Optional.of("baeldung"); opt.ifPresent(name -> System.out.println(name.length())); } In the above example, we use only two lines of code to replace the five that worked in the first example: one line to wrap the object into an Optional object and the next to perform implicit validation as well as execute the code.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Optional.html
Optional (Java Platform SE 8 )
April 21, 2026 - Indicates whether some other object is "equal to" this Optional. The other object is considered equal if: ... Returns the hash code value of the present value, if any, or 0 (zero) if no value is present. ... Returns a non-empty string representation of this Optional suitable for debugging.
🌐
Mkyong
mkyong.com › home › java8 › java 8 – convert optional to string
Java 8 - Convert Optional<String> to String - Mkyong.com
February 23, 2020 - In Java 8, we can use .map(Object::toString) to convert an Optional<String> to a String.
🌐
Medium
medium.com › @nweligalla › optional-in-java-8-5fbf12c90bfe
Optional in Java 8. Java Optional, introduced in Java 8… | by Nayana Weligalla | Medium
January 23, 2024 - Let’s rewrite the getText() function using Java Optional. There are two ways to create an Optional object with a value ‘Hello World. //option 1 Optional<String> text = Optional.ofNullable("Hello World");
🌐
GeeksforGeeks
geeksforgeeks.org › java › optional-tostring-method-in-java-with-examples
Optional toString() method in Java with examples - GeeksforGeeks
July 12, 2025 - Below programs illustrate toString() method: Program 1: ... // Java program to demonstrate // Optional.toString() method import java.util.*; public class GFG { public static void main(String[] args) { // create a Optional Optional<Integer> op ...
🌐
Medium
medium.com › @pratik.941 › mastering-optional-in-java-8-a-comprehensive-guide-3131eac600dc
Mastering optional in Java 8: A Comprehensive Guide | by Pratik T | Medium
May 30, 2024 - - Description: If a value is present, returns an `Optional` describing the value, otherwise returns an `Optional` produced by the supplying function. - Use Case: To provide an alternative `Optional` if the current one is empty.
Find elsewhere
🌐
Laulem
laulem.com › en › dev › optional-usage-java-tutorial.html
Guide To Java Optional - LauLem.com
November 7, 2024 - String element = "Alex"; Optional<String> myOptional = Optional.ofNullable(element); Optional<String> myOptional2 = Optional.ofNullable(null); Note: This method is preferable because it is null-safe. After creating an Optional, we can check if it is empty or not (value contained is null) using the isEmpty method (from Java 11) and isPresent.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-8-optional-class
Java 8 Optional Class - GeeksforGeeks
May 16, 2026 - Then, Optional.of(str[2]) creates an Optional that contains a non-null string. The print statements display an empty Optional and an Optional holding a value, showing how Optional safely represents both cases.
🌐
Medium
medium.com › walmartglobaltech › true-power-of-optional-in-java-306a77c62d00
True power of Optional in Java. Optional in Java is a container for an… | by Ginoy George | Walmart Global Tech Blog | Medium
July 19, 2021 - In this case, if the patronName contained a non-null value, that value will be printed, otherwise, the string ‘Anonymous’ will be printed. Isn’t this more concise than the null-check way? You can find many more examples of using Optional on the web, so let me focus on some of the anti-patterns of using Optional here.
🌐
Reddit
reddit.com › r/javahelp › how can i convert optional to string? (javafx)
r/javahelp on Reddit: how can I convert Optional <String> to String? (JavaFX)
November 20, 2017 -

In my program, it asks for your name by using a TextInputDialog, but it only works with Optional Strings, but I need a way to convert Optional <String> to String.

My Code.

    TextInputDialog dialogP1 = new TextInputDialog();
    dialogP1.setTitle("Player 1");
    dialogP1.setHeaderText("Player 1's Name");
    dialogP1.setContentText("Your Name");
    player1NameOptional = dialogP1.showAndWait();

I have tried this but that did not work.

   player1Name = (player1NameOptional.get());

Is there a way to convert Optional <String> to String? How?

🌐
BeginnersBook
beginnersbook.com › 2017 › 10 › java-8-optional-class
Java 8 Optional Class
September 11, 2022 - public class Example { public static ... System.out.print(str2); } } ... Optional.ofNullable() method of the Optional class, returns a Non-empty Optional if the given object has a value, otherwise it returns an empty Optional....
🌐
Develpreneur
develpreneur.com › home › introduction to the java optional class
Introduction to the Java Optional Class
November 15, 2022 - So this time, in the above code, we create a new Optional<String> for both the variable and method getName.
🌐
Hackajob
hackajob.com › talent › blog › using-the-optional-feature-in-java-8
Using the Optional Feature in Java 8
October 9, 2023 - In the example above, an ‘Optional String opStr’ is created with the text “Hello World”. A lambda expression is specified in the ‘ifPresent’ method that simply prints the input value.
🌐
DEV Community
dev.to › sohailshah › using-optionals-in-java-the-right-way-4aho
Using Optional in Java the right way - DEV Community
August 21, 2023 - Java //returning a empty optional of type string public static Optional<String> emptyOptional(){ return Optional.empty(); } //or Optional<String> str = Optional.empty(); Using the Optional.ofNullable() method we can create nullable optional objects. These objects can be null or have values in them ·
🌐
Baeldung
baeldung.com › home › java › java string › transforming an empty string into an empty optional
Transforming Empty Strings into Empty Optionals | Baeldung
February 20, 2026 - In Java 11, we’ll still use Optional.filter(), but Java 11 introduces a new Predicate.not() API that makes it easy to negate method references. So, let’s simplify what we did earlier, now using a method reference instead: @Test public void givenEmptyString_whenFilteringOnOptionalInJava11_thenEmptyOptionalIsReturned() { String str = ""; Optional<String> opt = Optional.ofNullable(str).filter(Predicate.not(String::isEmpty)); Assert.assertFalse(opt.isPresent()); }
🌐
CalliCoder
callicoder.com › java-8-optional-tutorial
Java Optional Tutorial with Examples | CalliCoder
February 18, 2022 - In this blog post, I’ll explain about Java 8’s Optional type and show you how to use it by giving simple examples. Optional is a container type for a value which may be absent. Confused?
🌐
javathinking
javathinking.com › blog › converting-options-string-to-string-java
Converting Optional String to String in Java — javathinking.com
It is a container object which may or may not contain a non-null value. When dealing with strings, you might often encounter situations where you have an Optional<String> and need to convert it to a regular String.