需求
List中存放若干学生对象(学生有学号,姓名等),去除List中重复的元素,并按学号降序输出。并利用LinkedHashSet集合,既不会重复,
同时有可预测的顺序即输入顺序。
补充
Set接口的一个实现类LinkedHashSet。
相对HashSet来说,LinkedHashSet存储结构是一个双向链表,因此它存储的元素是有序的。LinkedHashSet继承自HashSet,源码更少、
更简单,唯一的区别是LinkedHashSet内部使用的是LinkHashMap。这样做的意义或者好处就是LinkedHashSet中的元素顺序是可以保证
的,也就是说遍历序和插入序是一致的。
LinkedHashSet:
可以直接调用,这是系统已经封装好的一个类,可以给大家看一下源码,在实际中,我们只需知道它的用法即可,不需要它理解它是怎么一回事。
public class LinkedHashSet<E>
extends HashSet<E>
implements Set<E>, Cloneable, java.io.Serializable {
private static final long serialVersionUID = -2851667679971038690L;
/**
* Constructs a new, empty linked hash set with the specified initial
* capacity and load factor.
*
* @param initialCapacity the initial capacity of the linked hash set
* @param loadFactor the load factor of the linked hash set
* @throws IllegalArgumentException if the initial capacity is less
* than zero, or if the load factor is nonpositive
*/
public LinkedHashSet(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor, true);
}
/**
* Constructs a new, empty linked hash set with the specif