Avatar of Jay Roy
Jay RoyFlag for United States of America

asked on 

hashset uniquness

hi guys

Have a simple question about  hashset uniquness. An  Arraylist can contain  duplicate objects while a hashset cannot contain duplicate objects. How does  the hashset determine the uniquness of an object?

thanks
Java

Avatar of undefined
Last Comment
Ajay-Singh
Avatar of for_yan
for_yan
Flag of United States of America image

Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if this set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false.
Avatar of for_yan
for_yan
Flag of United States of America image

The above quote is of course from the API:
http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html
Avatar of for_yan
for_yan
Flag of United States of America image



This may be also interesting discussion though the answer to your question is mostly in the posts above:
http://www.coderanch.com/t/418129/java-programmer-SCJP/certification/HashSet-internal-working
Avatar of Jay Roy
Jay Roy
Flag of United States of America image

ASKER

CustomerVO cus= new CustomerVO();
cus.setId("1");
Set set = new Hashset();
set.add(cus);
...

Later on in my code, i try to add the same cus object again
set.add(cus),

what error will  be thrown?

thx

Avatar of for_yan
for_yan
Flag of United States of America image

According to API above there will be no Exception but if objects are equal the method will
return false, and the set will be unchanged
Avatar of for_yan
for_yan
Flag of United States of America image

So it remains to you to check if method .add(..) returned true or false
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of Ajay-Singh
Ajay-Singh

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo