How to make set to allow duplicate values... Please help me!!!
Hi Guys... I was asked in one of the interviews "How to make set to allow duplicate elements? " All i knew was we need to tweak the equals and hash code in some to make it happen but not sure how exactly it can be acheiveed. Can any one please post me an example, that would be great help for me. Waiting for your replies guys...thanks in advance
Re: How to make set to allow duplicate values... Please help me!!!
By definition, a Set contains no duplicate elements. So let me answer the question "How to make set to allow duplicate elements?" with a question - why would you want to? That's what other Collections are for (eg a List)
Re: How to make set to allow duplicate values... Please help me!!!
If you tweak the hash codes and other values of two Objects, they are no longer duplicates. Therefore, the phrasing of the question is somewhat irrelevant and not answerable.
Sets do not allow duplicate OBJECTS by nature.
Re: How to make set to allow duplicate values... Please help me!!!
Thanks for your time guys. I was not entirely correct on tweaking equals and hash code part and i know List can be used instead of set. But my intention is to know how to make a "set" to accept duplicate values.. I am still waiting for the answer if any one can help me out plzz...
Re: How to make set to allow duplicate values... Please help me!!!
You've already supplied one suggestion, so what have you done with it? Have you created a small program to play around with and test your assumptions? What have you tried? Post it here.
Also, word to the wise, "plzz" is not a word.
Re: How to make set to allow duplicate values... Please help me!!!
Quote:
But my intention is to know how to make a "set" to accept duplicate values
Sets are never designed to accept duplicate values as marked by KevinWorkman. If you really need to have duplicate values, use List or any other Collection.
Re: How to make set to allow duplicate values... Please help me!!!
Quote:
Originally Posted by
Mr.777
Sets are never designed to accept duplicate values as marked by KevinWorkman.
Nothing against Kevin, but I believe someone else pointed this out ;)
Re: How to make set to allow duplicate values... Please help me!!!
I really hate trick questions in an interview. It's a loose/loose situation. Either you get it wrong which only proves you are gullible, or you get it right by not answering the question which doesn't demonstrate much in the way of technical know how. Once, I even upset the interviewer by saying 'Oh, trick question. I can't answer this because there is no answer.' Didn't get the job because I bruised his ego.
Re: How to make set to allow duplicate values... Please help me!!!
Quote:
Originally Posted by
copeg
Nothing against Kevin, but I believe someone else pointed this out ;)
Yeah copeg pointed out too. Sorry copeg, forgot to mention you :P
Re: How to make set to allow duplicate values... Please help me!!!
Quote:
Originally Posted by
copeg
Nothing against Kevin, but I believe someone else pointed this out ;)
True. I actually think the answer that the interviewer was going for was for the OP to futz around with the equals() and hashcode() methods and that he should have written a small program to play with the effects of each on a Set. But yeah, in practice, don't use a Set if you want to allow duplicates.