【练习】List中存放若干学生对象(学生有学号,姓名,性别等属性),去除List中重复的元素,并按学号降序输出。

       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

测试结果: 

 【练习】List中存放若干学生对象(学生有学号,姓名,性别等属性),去除List中重复的元素,并按学号降序输出。_第1张图片

 

 

你可能感兴趣的:(练习题)