title: Simplified Java 8 Stream map entity to model
body: <p>I have two lists in Java, a list of models that I am receiving via a restful endpoint and a list of entities from my database. I am looping through all of the entities and updating the models with the appropriate ID generated after an insert. I need to do it this way because the model has other information that is not in the entity that I need to use later.</p>
  
  <p>This is what I am doing now and it works:</p>
  
  <pre class="lang-java prettyprint-override"><code>savedUserEntities.stream()
         .map(x -&gt;
                 uploadUserList.stream()
                         .filter(i -&gt; x.getUserName().equalsIgnoreCase(i.getUsername()))
                         .peek(i -&gt; i.setId(x.getId()))
                         .findFirst()
                         .orElse(null))
         .filter(Objects::nonNull)
         .collect(Collectors.toList());
  </code></pre>
  
  <p>I feel like it could be a little cleaner. Basically I am looping through the entities, finding the matching model, updating the model with the ID from the entity or setting it to null if there is no match, filtering out the resultant nulls, and then returning the updated models. Is there a cleaner way to do this same thing?</p>
  
  <p>Edit:</p>
  
  <p>This is my entity:</p>
  
  <pre><code>@Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  
  @Column(nullable = false)
  private String userName;
  private String firstName;
  private String lastName;
  private String password;
  
  @Column(nullable = false)
  private Long organizationId;
  
  private Long updatedDate;
  private Long createdDate;
  private Long userInactiveDate;
  private Long departmentId;
  private String batchId;
  private Long tempPasswordExpiresDate;
  </code></pre>
  
  <p>This is my model:</p>
  
  <pre><code>private Long id;
  private String firstName;
  private String lastName;
  private String username;
  private Long department;
  @JsonDeserialize(contentUsing = NestedDeserializer.class)
  private List&lt;Permission&gt; permissions;
  
  @Data
  @NoArgsConstructor
  private class Permission {
      private long locationId;
      private long roleId;
      private boolean all;
      private boolean assigned;
      private boolean unassigned;
  }
  </code></pre>