Hey im making a store for student details and i wanted some opinions on which collection to use. The store will have details such as name, number, address and email. The store will then be printed to a text file where i can load, save, edit and delete the details in the text file. I have never done this before and i do not no if there is any restrictions to file I/O when using collections. So i would really appreciate the comments. Thanks in advance.
4 Answers
If I were at your place,
then i would have created a bean class say Student
and for collection ArrayList<Student> student = new ArrayList<Student>();
and as ArrayList
is serialized so no issue for disk writing and a class
for all IO
operations.
For sorting ArrayList
by object property look

- 1
- 1

- 2,555
- 18
- 21
-
Only seen your post now man. Can an array list be sorted by name. it isnt asked in the brief but im thinking it could be a bonus to have a sorted text file. – Pendo826 Aug 02 '12 at 13:46
-
1@Pendo826 here http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property – Harmeet Singh Aug 02 '12 at 13:49
Unless some special "fast location" capabilities are required, such as search by last name or by student ID, a list would be an appropriate collection to use.
List<Student> students = new ArrayList<Student>();
If you do need to organize your students by some attribute, say, by student ID, consider using LinkedHashMap
:
Map<String,Student> studentById = new LinkedHashMap<String,Student>();
LinkedHashMap
gives you a predictable order of iteration. You could use a regular HashMap<K,V>
, but the order of iteration will be arbitrary. Finally, you could use a TreeMap<K,V>
, which would base the order of iteration on the ordering of the keys.

- 714,442
- 84
- 1,110
- 1,523
-
I think this will cause a serious issue in the Map case if he does not properly override equals / hashcode. – Woot4Moo Aug 02 '12 at 13:30
-
@Woot4Moo This is correct, if the OP uses his own class for keys he would need to deal with equals/hashCode. That's why I suggested using `String`s - they make excellent hash keys. – Sergey Kalinichenko Aug 02 '12 at 13:34
-
Well they make excellent keys insofar as the hash is guaranteed to be nice. http://www.infoq.com/presentations/effective-api-design/ to see Josh Bloch's opinion on Strings as Keys. It is quite informative for building APIs – Woot4Moo Aug 02 '12 at 13:39
-
In the brief it doesnt specify accessing the list or sorting it but do you think it would be beneficail to me to be able to sort it ? – Pendo826 Aug 02 '12 at 13:44
-
@Pendo826 I would not use a sorted or an associative collection unless I needed it. Note that you do not give up an ability to sort by using an unordered collection: `ArrayList` is perfectly sortable, so you will be able to sort it later "on demand", and use any combination of its attributes to re-order the entries in the list. – Sergey Kalinichenko Aug 02 '12 at 13:49
-
@dasblinkenlight. Thanks for the advice. Im just wondering should i run in to any problems when trying to delete etc... from a text file. I have it done using a normal hashmap store. But im worried i will run into serious problems when im trying to do this. Maybe i am over complcating things. But i would rather no what could go wrong before i start. – Pendo826 Aug 02 '12 at 13:52
-
1@Pendo826 The best thing to do when deleting from a file is to write a brand-new file from an in-memory collection, with the item in question removed, close it to make sure the data is in a consistent state, and then move it in place of the original file. This way you would not have your persistent data in an inconsistent state, even if the write fails. It is a bad idea to overwrite the same file, because you need to deal with extra characters left over from the previous write. This does not perform well for huge collections, but it should work well for a school project. – Sergey Kalinichenko Aug 02 '12 at 13:57
-
@dasblinkenlight Aw yeah i understand. There will only be four or five students in the store so i was planning on just overwriting the list everytime. I have to let the user delete and add students to the store as they please so hopefully it isnt to different from a hashmap store. – Pendo826 Aug 02 '12 at 14:00
Well if it has to be serializable, i.e. meaning you can write it to disk, you can use a List
. Now before anyone screams you can't serialize a List
that is correct, but you also cannot instantiate a List
either. Since we know all known subclasses of List
are serializable you can safely cast. In terms of how to store the data List<Student>
should be just fine.
EDIT
There seems to be some confusion here. In Object Oriented languages we know that the is-a
relationship holds true for objects specified in a hierarchy. So in the Java API we have an interface called List
this interface has classes that implement it ArrayList
for instance. This puts ArrayList
in the hierarchy of List
. Since ArrayList
implements Serializable
and we know that you cannot instantiate an object marked with the keyword interface (in Java). We can use casting to Serialize
any known implementation of List
. The reason why this will work is that the implementation (i.e. concrete object) that is passed around is guaranteed to be serializable.

- 23,987
- 16
- 94
- 151
-
ok so you cant serialize a list. So am i going to have problems saving and loading the store if i use a list? – Pendo826 Aug 02 '12 at 13:47
-
1@Pendo826 no. You can't even create a `List` you can create an `ArrayList` that implements Serializable (by default in the API) which `is-a` `List` . This is homework so I am not going to outright give an implementation. But this is one of the cornerstones of Object Oriented Languages. – Woot4Moo Aug 02 '12 at 13:50
In the simplest case a java.util.List
will do exactly what you want. However, if you want to be able to find entries in the collection quickly ( to support your update requirements ), you should also probably look at java.util.Map
. Map allows you to navigate quickly to a particular record without having to iterate over the entire collection, whereas with a List you'd have to look at every student in the collection in turn until you find the one you are interested in.

- 7,187
- 5
- 32
- 53