Complete the code to declare a simple DTO class with a private field and a getter.
public class UserDTO { private String [1]; public String getUsername() { return username; } }
The field name username matches the getter getUsername() following Java naming conventions.
Complete the code to create a constructor for the DTO that sets the username field.
public UserDTO([1] username) {
this.username = username;
}The username field is a String, so the constructor parameter must be of type String.
Fix the error in the method that converts an entity to a DTO by filling the blank.
public static UserDTO fromEntity(User user) {
return new UserDTO([1]);
}The DTO expects a username String, so we must pass user.getUsername() from the entity.
Fill both blanks to complete the DTO class with a setter and override toString method.
public class UserDTO { private String username; public void [1](String username) { this.username = username; } @Override public String [2]() { return "UserDTO{username='" + username + "'}"; } }
The setter method is named setUsername and the method overriding Object's string representation is toString.
Fill both blanks to create a method that converts a list of User entities to a list of UserDTOs.
public static List<UserDTO> fromEntityList(List<User> users) {
return users.stream()
.map(user -> new UserDTO([1]))
.collect(Collectors.[2]());
}We map each User to a UserDTO by passing user.getUsername(). Then we collect the results into a list using Collectors.toList().