This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Map

 
laoniu gougou
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Mudassar
thnx for the insight
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic