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

这篇博客介绍了如何使用LinkedHashSet在Java中去除List中包含学生对象(学号、姓名等属性)的重复元素,并按学号降序排列。LinkedHashSet结合了Set的无重复特性和链表的顺序性,确保了元素的遍历顺序与插入顺序一致。

需求

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
Java中,我们可以创建一个Student类,包含学号属性,然后定义一个双向链表(如LinkedList)结构来存储学生信息。这里我会提供一个基本的增改查操作以及链表按学号排序的示例: 首先,定义Student类: ```java public class Student { private int studentId; private String name; public Student(int id, String name) { this.studentId = id; this.name = name; } // getters and setters for studentId and name public int getStudentId() { return studentId; } public void setStudentId(int studentId) { this.studentId = studentId; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student{" + "studentId=" + studentId + ", name='" + name + '\'' + '}'; } } ``` 接下来,定义一个双向链表节点,包含Student对象: ```java class Node { Student student; Node prev; Node next; public Node(Student student, Node prev, Node next) { this.student = student; this.prev = prev; this.next = next; } } // 双向链表类 public class StudentList { private Node head; private Node tail; // 添加、修改操作... public void addStudent(Student student) { if (head == null) { head = new Node(student, null, null); tail = head; } else { Node current = head; while (current.next != null && current.next.getStudentId() < student.getStudentId()) { current = current.next; } if (current.next != null) { current.next.prev = current; } current.next = new Node(student, current, current.next); if (current.next == tail) { tail = current.next; } } } // 除操作... public void removeStudent(int id) { // 略 } // 修改操作... public void updateStudent(int id, String newName) { // 略 } // 按学号排序打印链表 public void sortAndPrintStudents() { if (head == null) { System.out.println("链表为空"); return; } Collections.sort(head, new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { return Integer.compare(s1.getStudentId(), s2.getStudentId()); } }); printList(); } private void printList() { Node current = head; while (current != null) { System.out.println(current.student); current = current.next; } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值