-
best data structure
Hi everybody. I'm trying to choose the best data structure to hold the following information:
I have a Student class, which consists of several data member and methods. I need to be able to process a list of thousands of Students in any order, but I also need to be able to reference each Student from the list individually. I will adding to and removing form the list very frequently.
Would an ArrayList make the most sense for this?
Thanks.
-
What do you mean by "in any order"? And what is the search key of the students? Some unique ID? Simply the array index (bad idea!)?
I recommend a binary tree, but depending on the finer points of your usage a hash table might be more appropriate. Besides, there's also the other issue of map or set: if you sort the objects by themselves you need a set (TreeSet, HashSet) and implement Comparable for Student (for TreeSet) or give Student.hashCode a meaningful override (for HashSet). If you sort by an external key you use a map (TreeMap, HashMap). Only the key type (probably a String or a Long) needs Comparable or hashCode, and both have it.