wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Map" Watch "Map" New topic
Author

Map

laoniu gougou
Greenhorn

Joined: Jan 25, 2001
Posts: 25
Which of the followings may allow duplicate elements:
1) Colletion
2)List
3)Set
4)Map

Answer if 1) and 2). Why not Map?
lee dalais
Ranch Hand

Joined: Feb 28, 2001
Posts: 67
hi there
this is from the java docs
"An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
"
Mudassar Shafique
Greenhorn

Joined: Mar 16, 2001
Posts: 10
Hi Lee,
The statement is saying that a key can map only one value. But different keys can map to same value. In this sense we can have multiple values in the map against multiple keys. The following program explains it:
import java.util.*;
public class mapclass {
public static void main(String[] args){

Map amap = new HashMap();
amap.put("key1", "Hello");
amap.put("key2", "Hello");
Object v1 = amap.get("key1");
Object v2 = amap.get("key2");
System.out.println(v1 + " " + v2);
}
}
So I think 4) map should also be chosen in the answer
Mudassar Shafique
Greenhorn

Joined: Mar 16, 2001
Posts: 10
However If you complete one key, value pair as an element in the map, then they are unique, as u can't have an element with the same key so as a whole(key + value) they will be unique.
lee dalais
Ranch Hand

Joined: Feb 28, 2001
Posts: 67
hi Mudassar
thnx for the insight
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Map