File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collection and Map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collection and Map" Watch "Collection and Map" New topic
Author

Collection and Map

Heba Mahmoud
Ranch Hand

Joined: Apr 26, 2009
Posts: 62
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
A) List
B) ArrayList
C) HashMap
D) TreeMap
E) HashSet
F) Arrays
G) LinkedList
H) Sets


Answer : D (TreeMap)
Explanation :
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.

source are : http://www.cafe4java.com/mockexams/scjp/mock3/a1.php

could you please show me why the true answer is D? even the question asks for a collection and map is not a collection.


SCJP Pass 85%....
Thousands of candles can be lighted from a single candle, and the life of the candle will not be shortened
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

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.
Heba Mahmoud
Ranch Hand

Joined: Apr 26, 2009
Posts: 62
where is the answer ?
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Heba Mahmoud wrote:where is the answer ?


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.


SCJP 6
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Thank you, Ankit, I am sure you remembered Bert's tip exactly. I appreciate that it is a simpler explanation and I will use it in the Introductory Java courses that I teach.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Larry Chung wrote:Thank you, Ankit, I am sure you remembered Bert's tip exactly.

I'm very bad at remembering things, sometimes I forget to get up in the morning
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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%|
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Abimaran Kugathasan wrote:Collection - Which is the core interface in java. (Somebody can tell that, Iterator is the core interface, because Collection extends Iterator)

Interface Collection extends interface Iterable, not Iterator. This is an important difference.

Any class that implements Iterable can be used in Java's for-each style loop.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
jQuery in Action, 2nd edition
 
subject: Collection and Map
 
Similar Threads
Sorting an array of objects
Huffman encoding/Decoding
Sorting HashMap by values
Questions on java.util package
Chishlom's question