package com.lovecoding.day18.ex01;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.function.Predicate;

public class TestStudent {

    @Test
    public void test01(){

        //创建集合
        Collection<Student> students = new ArrayList<>();

        //添加
        Student gqq = new Student(1, "高启强", 38);
        students.add(gqq); //@0x1122
        students.add(new Student(2,"小龙",22));
        students.add(new Student(3,"小虎",36));
        students.add(new Student(4,"阿盛",38));
        students.add(new Student(5,"阿伟",29));
        students.add(new Student(6,"高启强",29));

        //遍历
        for (Student student : students) {
            System.out.println(student);
        }

        //删除
        //存储引用数据类类型  存储地址值 @0x1122
        // new Student(1, "高启强", 38); 他的地址值 和 集合中的高启强 内容一样 地址不一样。
        // 删除根据地址值 删除
        boolean gqq1 = students.remove(new Student(1, "高启强", 38)); //0x1133
        System.out.println(gqq1);
        boolean gqq2 = students.remove(gqq); //@0x1122
        System.out.println(gqq2);


        //集合
        System.out.println(students);

        Integer age = gqq.getAge();

        //使用Collection的removeIf方法删除和自己年龄一样的组员
        students.removeIf(new Predicate<Student>() {
            @Override
            public boolean test(Student student) {
                return student.getAge() == age;
            }
        });

        //集合
        System.out.println(students);

        String name = gqq.getName();

        //使用Iterator遍历，并根据组长姓名删除组长
        Iterator<Student> iterator = students.iterator();

        while (iterator.hasNext()){
            Student s = iterator.next();
            //删除
            if (s.getName().equals(name)){
                iterator.remove();
            }
        }

        System.out.println(students);

        for (Student student : students) {
            System.out.println(student);
        }
    }


}
