啥也不多说上代码 :
说明:
List<Persion> 转 List<Student>
1.Persion.java
public class Persion { private String name; private String age; private String sex; public Persion(){} public Persion(String name, String age, String sex) { this.name = name; this.age = age; this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "Persion{" + "name='" + name + '\'' + ", age='" + age + '\'' + ", sex='" + sex + '\'' + '}'; } }
2.Student.java
public class Student { private String name; private String sex; public Student() { } public Student(String name, String sex) { this.name = name; this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
public class TestMain { public static void main(String[] args) { List<Persion> persions = getData(); persions.forEach( v->{ }); List<Student> studentList = persions.stream().map(v -> { return new Student(v.getName(), v.getSex()); }).collect(Collectors.toList()); } public static List<Persion> getData(){ List<Persion> arrayList = new ArrayList<>(); arrayList.add(new Persion("李四","20","男")); arrayList.add(new Persion("王麻子","30","男")); arrayList.add(new Persion("wangwu","10","女")); arrayList.add(new Persion("wangwu","10","女")); arrayList.add(null); return arrayList; } }