java8取出list中的對象的某一屬性
public class UserEntity implements Serializable { private Integer id; /** * 用戶名 */ private String userName; /** * 用戶手機號 */ private String phone; } public static void main(string args[]){ List<UserEntity> users=new ArrayList<>(); users.add(new UserEntity(1,"張三","18399990000")); users.add(new UserEntity(2,"王五","18399990023")); users.add(new UserEntity(3,"里斯","18399990005")); List<String> courseIds= users.stream().map(UserEntity::getUserName).collect(Collectors.toList()); }
最終的結果:[張三,王五,里斯]