通过map,返回部分属性。
MyUser,作为源数据
MyUserS,作为返回的新数据。
@Test
public void Test1()
{
List<MyUser> ulist=new ArrayList<>();
MyUser u1=new MyUser();
u1.setName("1");u1.setAge(1);u1.setHobby("swimming");
MyUser u2=new MyUser();
u2.setName("2");u2.setAge(2);u2.setHobby("sleeping");
MyUser u3=new MyUser();
u3.setName("3");u3.setAge(3);u3.setHobby("playing");
ulist.add(u1);ulist.add(u2);ulist.add(u3); List<MyUserS> usList=ulist.stream().map(x->{return new MyUserS(x.getName(),x.getAge());}).collect(Collectors.toList()); } @Data
public class MyUser
{
public String name;
public Integer age;
public String hobby;
} @Data
public class MyUserS
{
public MyUserS(String name,Integer age)
{
this.name=name;
this.age=age;
}
public String name;
public Integer age;
}