美文网首页码农的世界
【练习】List中存放若干学生对象(学生有学号,姓名,性别等属性

【练习】List中存放若干学生对象(学生有学号,姓名,性别等属性

作者: 超大大小猿 | 来源:发表于2019-07-07 21:45 被阅读0次

    List中存放若干学生对象(学生有学号,姓名,性别等属性),去除List中重复的元素,并按学号降序输出。(请百度并利用LinkedHashSet集合,既不会重复,同时有可预测的顺序即输入顺序)

    Student 1:

    package edu.xalead;
    import java.util.Objects;
    public class Student1 implements Comparable {
        private String name;
        private  int id;
        private int code;
        private char sex;
     
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Student1 student1 = (Student1) o;
            return id == student1.id &&
                    code == student1.code &&
                    sex == student1.sex &&
                    Objects.equals(name, student1.name);
        }
     
        @Override
        public int hashCode() {
            return Objects.hash(name, id, code, sex);
        }
     
        @Override
        public String toString() {
            return "Student1{" +
                    "name='" + name + '\'' +
                    ", id=" + id +
                    ", code=" + code +
                    ", sex=" + sex +
                    '}';
        }
     
        public Student1(String name, int id, int code, char sex) {
            this.name = name;
            this.id = id;
            this.code = code;
            this.sex = sex;
        }
     
        public String getName() {
            return name;
        }
     
        public void setName(String name) {
            this.name = name;
        }
     
        public int getId() {
            return id;
        }
     
        public void setId(int id) {
            this.id = id;
        }
     
        public int getCode() {
            return code;
        }
     
        public void setCode(int code) {
            this.code = code;
        }
     
        public char getSex() {
            return sex;
        }
     
        public void setSex(char sex) {
              this.sex = sex;
        }
     
        @Override
        public int compareTo(Object o) {
            if (o instanceof Student1) {
                Student1 e = (Student1) o;
                if (this.id > e.id) return -1;
                if (this.id < e.id) return 1;
                return 0;
            }
            throw new RuntimeException("类型不匹配无法比较大小");
        }
    }
    

    TestStudent1:

    package edu.xalead;
    import java.util.*;
     
    public class TestStudent1 {
        public static void main(String[] args) {
            List s = new ArrayList();
            s.add(new Student1("KAKA",14244,1001,'男' ));
            s.add(new Student1("JANNY",21243,1002,'女'));
            s.add(new Student1("CENDY",14324,1003,'男'));
            s.add(new Student1("HANY",84244,1004,'男'));
            s.add(new Student1("JANNY",21243,1002,'女'));
            s.add(new Student1("QUNY",12484,1005,'男'));
     
            System.out.println("未排序前:\n"+ s);
            Collections.sort(s);
            System.out.println("排序前:\n"+ s);
            LinkedHashSet w= new LinkedHashSet();
    //        w.add(new Student1("KAKA",14244,1001,'男'));
    ////        w.add(new Student1("JANNY",21243,1002,'女'));
    ////        w.add(new Student1("CENDY",14324,1003,'男'));
    ////        w.add(new Student1("HANY",84244,1004,'男'));
    ////        w.add(new Student1("JANNY",21243,1002,'女'));
    ////        w.add(new Student1("QUNY",12484,1005,'男'));
            for(int i=0;i<s.size();i++){
                w.add(s.get(i));
            }
            Iterator iter = w.iterator();
            System.out.println("筛选后:");
            while(iter.hasNext()){
                System.out.println(iter.next());
            }
            //System.out.println("筛选后:\n" + w);
        }
    }
    

    测试结果:


    image.png

    相关文章

      网友评论

        本文标题:【练习】List中存放若干学生对象(学生有学号,姓名,性别等属性

        本文链接:https://www.haomeiwen.com/subject/awnihctx.html