java - 在这种情况下应该使用哪个集合?

谁能建议我在这种情况下使用哪个集合:

每个学生都有一个付款历史记录,其中包含学生/家庭的付款详情。系统应确保学生帐户没有重复付款。该程序应该能够为学生添加付款详细信息,并确保不会注册重复的付款详细信息。

最佳答案

也许是 Map<Student, Set<Payment>>会做。

( Set 不允许重复。)

如果您覆盖 equals正确地(和 hashCode )你可以做类似的事情

Map<Student, Set<Payment>> studentPayments =new HashMap<Student, Set<Payment>>();

public void addStudentPayment(Student student, Payment payment) {

    if (!studentPayments.containsKey(student))
        studentPayments.put(student, new HashSet<Payment>());

    if (studentPayments.get(student).add(payment))
        System.out.println("Payment was added");
    else
        System.out.println("Duplicate found. Payment not added.");
}

https://stackoverflow.com/questions/7311934/

相关文章:

php - Wordpress - 按日期范围获取帖子

r - 在 R 中缩放数据忽略特定列

r - 分成3个字符长度

objective-c - 在 UIImageView 之上覆盖 UIImage,可能吗?示例代码?

android - 如何在小部件中使用 ListView ?

macos - sudo 命令后出现段错误

mobile - 移动客户端的 websockets

dependency-injection - 为什么用这么多术语来表达同样的事情? IoC 和 DI

c# - 有没有办法在没有 return 语句的情况下在函数中返回默认值?

ruby-on-rails-3 - 从 Rails 中的模型获取图像路径