• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ExamlLab Collections' Set

 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is suitable for storing elements that must not appear in the store more than once, if searching is not a priority

A. Set
B. Map
C. Hashtable

Answer is : A..Why ??
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Set- Doesnt allow repetition of values- No duplicates allowed and the elements are not ordered. If you had a similar requirement with ordering- you could choose TreeSet.

Map can contain duplicate keys, so does Hashtable

Update: The above statement is wrong.
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Map & Hashtable also doesn't allow duplicate elements know ??
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saibabaa Pragada wrote:Map & Hashtable also doesn't allow duplicate elements know ??


From the API: A map cannot contain duplicate keys; each key can map to at most one value. And Hashtable IS-A Map
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mohammed, A map cannot contain duplicate keys. Set cannot contain duplicate elements. So, I am thinking all the options should be correct. Did I miss any part that you are saying?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saibabaa Pragada wrote:Hi Mohammed, A map cannot contain duplicate keys. Set cannot contain duplicate elements. So, I am thinking all the options should be correct. Did I miss any part that you are saying?


Ah Am really sorry- Misread the question and details . Its given that: if searching is not a priority - So we can think of using Set. So the second details given is important to note.
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if searching is not a priority, Why we should think of using Set ? I am not clear on the logic behind that
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saibabaa Pragada wrote:if searching is not a priority, Why we should think of using Set ? I am not clear on the logic behind that


Maps help in searching by using the keys. For Set- You would have to iterate through the set.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic