This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

collection doubt again  RSS feed

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to store elements in a collection that guarantees that no duplicates are stored. Which two interfaces provide that capability? (Choose Two)
A. java.util.Map
B. java.util.Set
C. java.util.List
D. java.util.StoredSet
E. java.util.StoredMap
F. java.util.Collection
Ans: B,D�.

Doubt A,E also guarantee unique values

pls help
regards,
gitesh
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

For Map,wheather two objects are equal or not ,depends on equals method.If you won't override equals method ,no two objects are never equal,even if they are equal.

Thanks

Anil Kumar
 
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Anil, but the same holds for Set and SortedSet - if the equals() method of the objects you put in always returns false, even if the objects are really equal, then you can store duplicates in a Set. It would be an ugly hack, and really a bug in your equals() method if you do this.

A Map stores key-value pairs. The keys have to be unique, but the values do not have to be unique. So a Map doesn't protect against duplicate values. However, the question is a little bit ambiguous. Indeed, you cannot have duplicate key-value pairs in a map.

(Note: Please quote your sources).
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesper,

i am also confused with the question. The source is JavaChina.

What you told I came to understand that Map assures unique Key's and not values, so the answer here is not Map, but Set.

Gitesh
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Page 539, Chapter 7 of K&B states:

Collections come in four basic flavors:
- Lists Lists of things (classes that implement List).
- Sets Unique things (classes that implement Set).
- Maps Things with a unique ID (classes that implement Map).
- Queues Things arranged by the order in which they are to be processed.

That points to Set and Sorted set as the answer.

Maps store unique IDs, but IDs are not the elements you store, they are only identifiers you use to find your elements. Maps will store duplicate elements and Sets will not.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gitesh

Set and its implementations like SortedSet always ensure that only unique elements are there in the collection.

Try this handson example :



This will add to uniques Set only if the element is new and if its repeated it will figure in duplicate Set
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Ranchers!!. Doubt cleared.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!