top of page

java streams: working with optionals map function

java optionals are a useful feature introduced in java 8 that allows developers to handle null values in a more elegant way. they provide a way to represent the absence of a value in a null-safe manner, allowing developers to avoid the need to constantly check for null values and throw null pointer exceptions.


one of the useful methods provided by optionals is the map function. This function allows you to apply a function to the value contained within an optional, and return the result as a new optional. this can be particularly useful when you want to perform some operation on the value contained within an optional, but you want to ensure that the operation is only performed if the value is present.


for example, suppose you have a method that returns an Optional containing a string, and you want to convert that string to uppercase if it is present:


public Optional<String> getString() {
  // Some code that returns an Optional<String>
}

public Optional<String> toUpperCase() {
  return getString().map(String::toUpperCase);
}

in this example, the map function is used to apply the touppercase method to the string contained within the optional returned by getstring. If the optional contains a value, the function will be applied and the result will be returned as a new optional. If the optional is empty, the map function will simply return an empty optional.


here are a few more examples of the map functions

Optional<String> optionalString = Optional.of("hello");
Optional<String> transformedString = optionalString.map(String::toUpperCase).map(s -> s + " world");

Optional<String> optionalString = Optional.of("   hello   ");

Optional<String> transformedString = optionalString.map(String::trim)
                                                 .map(String::toUpperCase)
                                                 .map(s -> s.substring(0, 3))
                                                 .map(s -> s + " world");


record User {
  private String name;
  private int age;
}

record UserDTO {
  private String name;
  private int age;
}

public Optional<User> getUser() {
  // Some code that returns an Optional<User>
}

public Optional<UserDTO> getUserDTO() {
  return getUser().map(user -> new UserDTO(user.getName(), user.getAge()));
}


Optional<String> optionalString = Optional.of("   hello   ");

Optional<String> transformedString = optionalString.map(String::trim)
                                                 .map(String::toUpperCase)
                                                 .map(s -> s.substring(0, 3))
                                                 .map(s -> s + " world");

in summary, java optionals are a useful tool for handling null values in a safe and concise way. the map function, in particular, allows you to perform operations on the contained value in a null-safe manner, while still allowing you to handle the absence of a value if needed. in my opinion it's more readable then classic if conditions with indents. also i like the chaining of the map functions. obviously chaining string operations is also possible, but i think you got the idea.

6 views0 comments

Recent Posts

See All
bottom of page