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());
            });
        });




    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM