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
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:-
posted 4 years ago
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!