Create and initialize HashMap inline in a lambda stream

Working with Spring Integration I had the need to create MessageHeaders class in a Lambda expression.The MessageHeaders object requires an HashMap as constructor parameter and the inline initializations of an HashMap is not the most intuitive. The solution:

public List<Message> handleFileRequest() {
// we create a list from an enumerator
List<Message> messageList =
      // we read the file content from an external provider
      // for each object we create a new message
      .map(file -> MessageBuilder.createMessage(file,
         new MessageHeaders(new HashMap<String, Object>() { {
        put("fileName", file.getTitle());
      } })))

  return messageList;

In detail:

new HashMap<String, Object>() { {put("valueText", Object} }

The first brace ({ }) creates an Anonymous Inner Class, the second brace initializes a block inside the Anonymous Inner Class. This Java notation is not new but very unusual.

You can consider to call an external method to generate your hashmap.