java8 list 删除元素 结构复杂版

实体类

School.java

package com.company.domain;

import java.util.List;

/**
 * @author :lichuankang
 * @date :2020/9/3 11:02
 * @description :学校
 */
public class School {

    private String name;

    private List<Depart> departList;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Depart> getDepartList() {
        return departList;
    }

    public void setDepartList(List<Depart> departList) {
        this.departList = departList;
    }
}

Depart.java

package com.company.domain;

import java.util.List;

/**
 * @author :lichuankang
 * @date :2020/9/3 11:03
 * @description : 组织
 */

public class Depart {

    private String name;

    private List<Student> students;

    public Depart() {

    }

    public Depart(String name, List<Student> students) {
        this.name = name;
        this.students = students;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }
}

Student.java

package com.company.domain;

/**
 * @author :Created by lichuankang
 * @date :Created in 2020/7/28 14:10
 * @description :
 */
public class Student {

    private Integer id;
    private String name;
    /**
     * 学生状态   1 正常  2 休学
     */
    private Integer status;



    public Student() {
    }

    public Student(Integer id, String name, Integer status) {
        this.id = id;
        this.name = name;
        this.status = status;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }
}

FlatMapTest.java

package com.company;

import com.company.domain.Depart;
import com.company.domain.School;
import com.company.domain.Student;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author :lichuankang
 * @date :2020/9/3 11:07
 * @description :
 */
public class FlatMapTest {

    public static void main(String[] args) {
        // 构造数据
        School lickSchool = new School();
        lickSchool.setName("测试小学");

        List<Depart> departList = new ArrayList<>();

        List<Student> studentList = new ArrayList<>();

        studentList.add(new Student(1,"测试1",1));
        studentList.add(new Student(2,"测试2",1));
        studentList.add(new Student(3,"测试3",1));
        studentList.add(new Student(4,"测试4",2));
        studentList.add(new Student(5,"测试5",1));
        studentList.add(new Student(6,"测试6",2));

        List<Student> studentList2 = new ArrayList<>();

        studentList2.add(new Student(11,"测试11",1));
        studentList2.add(new Student(12,"测试12",1));
        studentList2.add(new Student(13,"测试13",1));
        studentList2.add(new Student(14,"测试14",2));
        studentList2.add(new Student(15,"测试15",1));
        studentList2.add(new Student(16,"测试16",2));


        List<Student> studentList3 = new ArrayList<>();

        studentList3.add(new Student(121,"测试121",1));
        studentList3.add(new Student(122,"测试122",1));
        studentList3.add(new Student(123,"测试123",1));
        studentList3.add(new Student(124,"测试124",2));
        studentList3.add(new Student(125,"测试125",1));
        studentList3.add(new Student(126,"测试126",2));


        departList.add(new Depart("纪委部",studentList));
        departList.add(new Depart("魔术部",studentList2));
        departList.add(new Depart("舞蹈部",studentList3));

        lickSchool.setDepartList(departList);


        lickSchool.getDepartList().stream().map(Depart::getStudents).forEach(students -> {
            Iterator<Student> iterator = students.iterator();
            while (iterator.hasNext()) {
                Student student = iterator.next();
                if (student.getStatus().equals(2)) {
                    System.out.println(student.getName() + "-->" + student.getStatus());
                    iterator.remove();
                }
            }

        });

        lickSchool.getDepartList().stream().map(Depart::getStudents).forEach(students -> {
            students.forEach(student -> {
                System.out.println(student.getName() + "-->" + student.getStatus());
            });
        });




    }
}

posted @ 2020-09-03 13:52  lick  阅读(3628)  评论(2编辑  收藏  举报