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: Question on Set

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Question on Set

    In a set if we have 5 objects {'a','v','d','w','r'}
    And if i add one more same element say 'd', will the earlier 'd' object be retained in Set or it will be replaced by new 'd' object.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Question on Set

    Thread moved to http://www.javaprogrammingforums.com...ions-generics/

    Did you read the API on Set? To quote the doc on the add method

    If this set already contains the element, the call leaves the set unchanged and returns false

  3. #3
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Question on Set

    Hello tcstcs!
    The javadoc for Set.add(E e) says;
    "Adds the specified element to this set if it is not already present (optional operation). More formally, adds the specified element e to this set if the 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. In combination with the restriction on constructors, this ensures that sets never contain duplicate elements."

    From what I get from the above, I would answer to your question that the new 'd' will never be added to the set, therefore the earlier 'd' will be retained.
    You could also take a look at an interesting discussion about duplicates in sets that I found.
    Hope it helps.

  4. #4
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Question on Set

    HashSet is implemented with HashMap. So in HashMap, if i add element with existing key the older value gets replaced with new one. Then in set as its based on HashMap, older value should be replaced with new one rite?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Question on Set

    should be replaced
    Write a small test program and see what happens.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Question on Set

    with the program, we will not be able to see the results. Reason being
    If set has {a,s,d,f}
    I will add 's' to set and after addition it will be {a,s,d,f}
    So we will not know whether its prev 's' or new 's'.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Question on Set

    By comparing the references to the obects using the == operator.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Question on Set

    I wrote this program but its giving o/p as false,false only without duplicates.
    public static void main(String[] args)
    {
    HashSet hh = new HashSet();
    String aa = new String("a");
    String bb = new String("b");
    hh.add(aa);
    hh.add(bb);

    Iterator it = hh.iterator();
    while(it.hasNext())
    {

    System.out.println("---"+aa==it.next());
    }

    }

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Question on Set

    What is printed out by your program? Is that what you expected to see?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question on Set

    IF you make a set of with Set<object> declaration u need to make sure that Class(for object) overrides the hash codes and equals method correctly else duplicates will be allowed. when you are working with Set<String> it will not allow you to add "A" or "D" two times as the String class has hashcode and equals method implemented.