Results 1 to 4 of 4
  1. #1
    Bulska is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default TreeSet weirdness

    I have a question regarding the TreeSet of the Java Collections Framework.

    I'm working in Netbeans IDE 6.9.1.

    The problem I'm having is that I'm creating and adding new objects to a TreeSet in a while loop, but that some newly created objects are regarded as being already in the TreeSet!

    The part where a new HuffKnoop is created is the bit where the issue is at.

    Code:
    Java Code:
    public HuffKnoop makeHuffmanTree(TreeSet<HuffKnoop> tSH)
        {
            // Making huffman tree
            while (tSH.size() > 1)
            {
                System.out.println("=========================");
                for (HuffKnoop test : tSH)
                {
                    if (test.leftChild != null) System.out.println(test.leftChild.karakter + "" + test.rightChild.karakter + ": " + test.frequentie);
                    else System.out.println(test.karakter + ": " + test.frequentie);
                }
                System.out.println(tSH.size());
                
                HuffKnoop links = tSH.first();
                tSH.remove(links);
                
                HuffKnoop rechts = tSH.first();
                tSH.remove(rechts);
                
                HuffKnoop HK = new HuffKnoop();
                HK.frequentie = links.frequentie + rechts.frequentie;
                HK.leftChild = links;
                HK.rightChild = rechts;
    
                System.out.println(tSH.contains(HK));  // ======== THIS RETURNS  TRUE (?)
    
                System.out.println("HK: " + HK.leftChild.karakter + HK.rightChild.karakter + ": " + HK.frequentie);
                
                tSH.add(HK);
            }
            
            return tSH.first();
        }

    |=====|
    The odd thing is that it only returns true when both HK.leftChild.karakter and HK.rightChild.karakter are a character and not empty.

    I really have no idea what the TreeSet is doing, can anyone help me out with this?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    but that some newly created objects are regarded as being already in the TreeSet!
    Which is the purpose of a Set - contains unique objects, the uniqueness based upon the hasCode() and equals() methods (see Set (Java Platform SE 6) ). Given the class you use isn't part of the standard java packages, I can't say what defines the equality. If you want duplicates, use a List or wrap the object into a custom class

  3. #3
    Bulska is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    How can you change how the equality is checked for a custom class?

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    As I mentioned in my previous post, read the API. It explains all of this in detail, especially the portion of my previous post which alludes to how a Set works - via the equals and hashcode methods. As with inheritance, if an object does not define these methods it relies on a parent class - which is Object.equals() and Object.hashCode() if no parent class overrides this method (again, read the API for these methods to understand how they work)

Similar Threads

  1. problems with a TreeSet
    By j2me64 in forum Advanced Java
    Replies: 1
    Last Post: 01-10-2011, 04:40 PM
  2. Please Help - TreeSet
    By Riftara in forum New To Java
    Replies: 1
    Last Post: 10-21-2010, 09:33 PM
  3. TreeSet Demonstration
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:34 PM
  4. ClassCastException in TreeSet
    By pHew in forum New To Java
    Replies: 2
    Last Post: 01-16-2008, 01:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •