GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes collection's 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 "collection Watch "collection New topic
Author

collection's

fraternity lawson
Greenhorn

Joined: Mar 11, 2001
Posts: 26
Is the following statement true for the class java.util.HashSet?
The elements in the collection are guaranteed to be unique?.
James Du
Ranch Hand

Joined: Mar 23, 2001
Posts: 186

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

The above quote is from API specification regarding interface set.
From that we can conclude the answer to your question depends on the defination of "unique", interface set just guarantees that e1 and e2 would not both appeared in the collection if e1.equals(e2) return true.
Hope that helps
James
[This message has been edited by James Du (edited May 08, 2001).]
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
Hi Fraternity,
The answer to your specific question is TRUE , the reason is that Set interface is implimented by HashSet class so, all the properties of Set are inheritted by the HashSet and it must deliver the contract of Set interface . The contract of Set interface is correctly
quoted by James, so I am not quoting the same for want of space.
However, you may want to see the entire discription of the same
in the API.
Hope this clears your doubt.
Ravindra Mohan.
[This message has been edited by Ravindra Mohan (edited May 08, 2001).]
 
GeeCON Prague 2014
 
subject: collection's