class User {
public Integer id;
public String username;
public User(Integer id, String username) {
this.id = id;
this.username = username;
}
}
public static void main (String[] args) {
List<User> userList = new ArrayList<>();
userList.add(new User(1, "admin"));
userList.add(new User(2, "guest"));
userList.add(new User(3, null));
Map<Integer, String> idsUsernamesMap =
userList.stream().collect(Collectors.toMap(user -> user.id, user -> user.username));
idsUsernamesMap.forEach( (k,v) -> System.out.println("id: " + k + "; username: " + v));
}
Login in to like
Login in to comment