简体   繁体   中英

Take an attribute from a list of pojo class to an array of that attribute

Suppose for one field/attribute in a pojo class , i need to create a list of that field's value alone. Its doable in java using iteration . But is there any custom library for doing this ? For eg :

public class User
{
  private String name;
  private Integer age;
  // setters and getters. 
}

What i want is , List<User> ---> List<String> , here the values are of field "name".

In Java 8 you can create a stream from the list and apply a mapping function to map it to a list of names:

List<User> users;
List<String> names = users.stream().map(user -> user.getName()).collect(Collectors.toList());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM