You are over-complicating things. If you have an Optional<String> optionalValue you can simply say:
if(doTrim) optionalValue=optionalValue.map(String::trim);
and proceed afterwards, e.g. call get on it.
But if you are calling get() unconditionally as in your example, you have to be confident, that the Optional isn’t, well, optional, but present. If you know that the String is present, you can do it even simpler:
String s=optionalValue.get();
if(doTrim) s=s.trim();
If you insist on having all the code inline, you can, of course, write it like:
(doTrim? optionalValue: optionalValue.map(String::trim)).get()
or
(doTrim? optionalValue.get(): optionalValue.get().trim())
But there is no real advantage over an ordinary if statement here. If you have a real optional value, not knowing whether the String is present, and don’t want to call get immediately, I’d recommend the first version of my answer as it allows to proceed with the Optional in any way you like. Your variant of selecting between String::trim and an identity function may look more funky but has no real advantage over conventional programming.