2

Can any one suggest me which collection to use for this scenario:

Every student has a payment history with details of payment made by the student / family. The system should ensure that no duplicate payments are there against a student account. The program should be able to add payment details for a student, and ensure that duplicate payment details are not getting registered.

aioobe
  • 413,195
  • 112
  • 811
  • 826
satheesh
  • 1,443
  • 7
  • 28
  • 41
  • 2
    I don't think the collection type is going to matter. I think you should perform the duplicate check against the collection yourself rather than relying on the collection to do it. – Rup Sep 05 '11 at 19:16

5 Answers5

6

Perhaps a Map<Student, Set<Payment>> would do.

(A Set won't allow for duplicates.)

If you override equals properly (and hashCode) you can do something like

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.");
}
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • that requires me to see the rest of your person / payment clasess. Besides, I think you should post a new question for that, giving the details. If you post a link to the question here, I'll have a look at it. – aioobe Sep 05 '11 at 19:27
  • its just a employment program where there will be savings account and related account for that in the name of a student.And based on his saving every month there will be a credit of 75% in related employment program account. – satheesh Sep 05 '11 at 19:32
  • I think the best advice I can give you is to have a look at [this](http://stackoverflow.com/questions/27581/overriding-equals-and-hashcode-in-java) question, and post a more specific follow-up question if that doesn't provide enough info. – aioobe Sep 05 '11 at 19:42
  • ok Mr aioobe i will try working on that ..if i have anything to clarify i will post ... – satheesh Sep 05 '11 at 19:46
3

whenever you have a requirement for no duplicates, Use a Set. If you use a HashSet, make sure to implement hashCode on the Objects you put in the set (and also equals).

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
1

You might find a Map of students to a set of payments helpful

Map<Student, Set<Payment>> studenthistory;
aioobe
  • 413,195
  • 112
  • 811
  • 826
1

You can consider e.g.

Map<Student, HashSet<Payment>> students;

Student is the student, identified by name or some ID. HashSet<Payment> are the payments. A Payment contains an ID, amount, date, etc.

Jiri Kriz
  • 9,192
  • 3
  • 29
  • 36
0

A set would be your best choice. A set is a collection that contains no duplicate elements.

Just remember to override the equals-method on your class: http://www.javapractices.com/topic/TopicAction.do?Id=17

In your case, it will be something like:

public boolean equals(Object obj) {
    if (!obj instanceof Payment)
        return false;
    }
    Payment p = (Payment) o;
    return p.getId().equals(getId());
}

.... or something like that :)

fireshadow52
  • 6,298
  • 2
  • 30
  • 46
jorgen.ringen
  • 1,285
  • 5
  • 14
  • 20