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

List类

package util.xta;

import java.util.Objects;

public class Lo {
public Lo(String Name, int ID){
this.Name=Name;
this.ID=ID;
}
private String Name;
private int ID;

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

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Lo lo = (Lo) o;
    return ID == lo.ID &&
            Objects.equals(Name, lo.Name);
}

@Override
public int hashCode() {
    return Objects.hash(Name, ID);
}

@Override
public String toString() {
    return "List{" + "Name='" + Name + '\'' + ", ID=" + ID + '}';
}

}

实验类
package util.xta;

import java.util.*;

public class LoTest {
public static void main(String[] args) {
List x=new ArrayList();
x.add(new Lo(“小三”,9782));
x.add(new Lo(“dadie”,7865));
x.add(new Lo(“小写”,782));
x.add(new Lo(“倒萨打完”,4545));
x.add(new Lo(“我的娃”,4782));
x.add(new Lo(“小三”,9782));
x.add(new Lo(“小写”,4972));
x.add(new Lo(“小小”,89879));
Collections.sort(x, new Comparator() {
@Override
public int compare(Lo o1, Lo o2) {
return o2.getID()-o1.getID();
}
});
LinkedHashSet m=new LinkedHashSet();
m.addAll(x);
Iterator q=m.iterator();
while (q.hasNext()){
System.out.println(q.next());
}

}

}

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