This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

how to ensure unique values in my set  RSS feed

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello every one!
I am working on a code where i have a condition that no duplicate values to be entered. I have a quiz module where next and previous buttons are given. Questions are saved when we press next in the module. Now if the duplicate question and answers are entered it must be omitted. And for this i tried to have=

output=Size of Set=2
now i know two objects have different hashcode but now how to i ensure uniqueness.
Please guide me.It will be very useful to me and i will be highly thankful to you
 
Greenhorn
Posts: 27
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question needs to override the equals() and the hashCode() methods.
 
Marshal
Posts: 59783
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are never duplicate values in a java.util.Set. Not unless it has been implemented wrongly. Your question1 and your question are different because you have given then different hash codes. As you have been told, you override hashCode and equals but not to prevent duplicates. You override them to “tell” the Set object that they are the same.
Also you will have problems if question and question1 are fields. If you reassign them, or if you change any of the state of the objects in the Set, you mighty never find them again. You can get false true from this sort of code:-
 
Ankush Seth
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You mr.Campbell Ritchie and mr.Michael Romero
I got the way to ensure uniqueness . Thank you very much for your quick and useful reply!
 
Campbell Ritchie
Marshal
Posts: 59783
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Campbell Ritchie
Marshal
Posts: 59783
188
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you know about equals methods: look at Joshua Bloch's Effective Java chapter 3, Odersky, Spoon and Venners, and Angelika Langer's Java equals and hashCode FAQ.
 
Ankush Seth
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you once again !!
 
Ankush Seth
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These links are amazing , my knowledge has increased after reading those articles.
I just don't know how to thank you!
 
lowercase baba
Bartender
Posts: 12627
50
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure that will be as easy as you think. If the answers are the same but in a different order, is that a different question? What about using different case letters here and there?

If someone words the question differently, is it a different question?
If the question is worded slightly differently, is it a different question?
 
Ankush Seth
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you mr.Fred Rosenberger for pointing out the bugs. I will soon look into it,but my 60% problem is solved and rest will be solved if i remove the bugs as suggested by you.
Thank you once again!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!