Home  >  Q&A  >  body text

java - bean collection split into multiple lists

Now there is a bean list that needs to be split into multiple member lists. How to write it better?

public class User {
    private String name;
    private String age;
    //..getter,settter省略
}
List<User> data = ...
List<String> name = data里的所有name
List<String> age = ...
迷茫迷茫2683 days ago739

reply all(2)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-17 10:10:35

    List<String> name = data.stream().map(User::getName).collect(Collectors.toList());
    List<String> age = data.stream().map(User::getAge).collect(Collectors.toList());

    reply
    0
  • PHP中文网

    PHP中文网2017-05-17 10:10:35

    Map<String,User> should satisfy you. The key is name and the value is placed directly in the bean

    reply
    0
  • Cancelreply