This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Which collection would be the best one to use, if you were working with a Student Information management web application that needs to maintain and search students, stored as instances of a class 'Student'.
You should be able to perform the following:
i) identify a Student by its unique alphanumeric identification number.
ii) Print out the Student in order of their unique identification numbers
B) ArrayList C) HashMap D) TreeMap E) HashSet F) Arrays
G) LinkedList H) Sets
Answer : D (TreeMap)
Option D is correct. Both HashMap and TreeMap enable storing a 'key' and a 'value' combination, where the values and keys can be objects, and the objects are searchable on their keys. However, only TreeMap maintains the natural order of the keys.
Map do not implements collection interface but still it is considered as a part of collection API.
Because answer includes the treemap and it is the appropriate collection as per the requirements so it is valid to choose treemap.
Harpreet gave a good explanation and the mock exam gave the right answer as Option D. Unfortunately, Sun caused some confusion back in Java 1.2 when it created the Collections Framework which consists of Collection classes and Map classes. Since TreeMap is part of the Collections Framework, it is loosely called a collection although it is a descendent of Map. Very crazy.
I don't remember exactly, but if I remember correctly, Bert once said that if the question writes "Collection" (with uppercase C), then you have to choose a sub-type of java.util.Collection. But if the question says "collection" (with lowercase c), then the answer can be a java.util.Collection or a java.util.Map. Maybe I remember it wrong, so this information is not reliable. Generally a java.util.Map implementation is the right answer in cases where you need some sort of identifier with elements in the collection. The key works as the identifier in Maps...
Map is a part of collections API frame work. collections, Collection, Collections are different.
collection - is a frame work in java
Collections - A utility class in java, to manipulate the collections
Collection - Which is the core interface in java. (Somebody can tell that, Iterator is the core interface, because Collection extends Iterator)
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|