Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: How to make set to allow duplicate values... Please help me!!!

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,226
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default 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)

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default 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.

  4. #4
    Junior Member
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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...

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How to make set to allow duplicate values... Please help me!!!

    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.

  7. #7
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,226
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: How to make set to allow duplicate values... Please help me!!!

    Quote Originally Posted by Mr.777 View Post
    Sets are never designed to accept duplicate values as marked by KevinWorkman.
    Nothing against Kevin, but I believe someone else pointed this out

  8. #8
    Forum VIP
    Join Date
    Jun 2011
    Posts
    273
    My Mood
    Bored
    Thanks
    39
    Thanked 75 Times in 63 Posts
    Blog Entries
    4

    Default 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.

  9. #9
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How to make set to allow duplicate values... Please help me!!!

    Quote Originally Posted by copeg View Post
    Nothing against Kevin, but I believe someone else pointed this out
    Yeah copeg pointed out too. Sorry copeg, forgot to mention you

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: How to make set to allow duplicate values... Please help me!!!

    Quote Originally Posted by copeg View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Removing Duplicate Values From an Array
    By nicsa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 30th, 2011, 06:55 PM
  2. How to make Java determine if a values is an integer or not.
    By MiniatureBeast in forum Object Oriented Programming
    Replies: 5
    Last Post: August 17th, 2011, 11:32 PM
  3. Duplicate elements in 2 Arraylist
    By tcstcs in forum Collections and Generics
    Replies: 3
    Last Post: April 18th, 2011, 12:56 AM
  4. duplicate t:dataTable
    By smackdown90 in forum Web Frameworks
    Replies: 0
    Last Post: August 4th, 2010, 10:35 AM
  5. Eliminating duplicate values
    By Harry_ in forum Collections and Generics
    Replies: 7
    Last Post: November 9th, 2009, 05:35 AM

Tags for this Thread