• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collection and Map

 
Heba Mahmoud
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where is the answer ?
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15448
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic