I struggle with finding an elegant way to convert a variable of type Optional<String[]>
to Optional<String>
and joining all elements of the given array.
Is there an elegant solution for this?
Optional<String[]> given = Optional.ofNullable(new String[]{"a", "b"});
Optional<String> joinedString = ....;
Assertions.assertThat(joinedString.get()).isEqualTo("ab");
Looks to me like a simple map
operation with String.join()
.
Optional<String[]> given = Optional.ofNullable(new String[]{"a", "b"});
var joinedString = given.map(s -> String.join("", s));
System.out.println(joinedString.get()); // prints "ab"