简体   繁体   中英

What collection to use in java for storing multiple objects that have the same hashcode?

Let's say i want to store records for each class a student attends. Both student and class have unique identifiers, but multiple students can attend same class and a student can attend multiple classes.

I want to arrange those records in such manner that i don't have to search all the records with complexity O(n), but all the objects that have the same class id resolve in the same slot, much like a hashtable works, except i find that java HashSet does not support duplicates.

So my problem is next...i want to return a collection of all those records whose hashcode has resolved to the same location in the table, but that data structure would have to support duplicates ofcourse, because multiple students can attend class x. One such slot would be a list of all records that resolved to the same slot.

Addressing the general issue of hashcodes first.

Hash tables in general work will still work if you have many distinct keys that map to the same hashcode. However, hash tables are 1-to-1 maps. They map each distinct key to one (and only) records / entry. In the Java context, this applies to all Map collections, and to all Set collections as well ... modelling a set as a degenerate form of map.

If you want one key to map to (potentially) multiple different records / value, then you need a multi-map data structure. This can be simulated (using the Java collection types) as a Map<K, List<V>> or Map<K, Set<V>> .

To summarise:

  1. It is the distinctness of the keys that matter, not the distinctness of hashcodes. (A hash table can deal with hashcode collisions.)

  2. If you have non-distinct keys then you need a multi-map.


Looking at your particular use-case, what you appear to have is a set of attendance records that have two external keys; ie a class id and a source id. (I'm supposing that each attendance record consists of some data that represents the students attendance at classes.)

The fact that you have two keys here implies that you want to query by both at different places in your application; eg "find attendance records for student X", "find all attendence records for class Y".

This implies that you actually need 2 multi-maps to support those queries; eg Map<StudentID, <Set<AttendanceRecord>> and Map<CourseID, <Set<AttendanceRecord>> .

There are a couple of invariants that you will need to maintain. The sets must (of course) only contain AttendanceRecord objects that pertain to the respective student or course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM