Results 1 to 4 of 4
Thread: TreeSet weirdness
- 03-18-2011, 01:19 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
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?
- 03-18-2011, 02:03 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
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 classbut that some newly created objects are regarded as being already in the TreeSet!
- 03-18-2011, 02:59 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
How can you change how the equality is checked for a custom class?
- 03-18-2011, 03:29 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
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
-
problems with a TreeSet
By j2me64 in forum Advanced JavaReplies: 1Last Post: 01-10-2011, 03:40 PM -
Please Help - TreeSet
By Riftara in forum New To JavaReplies: 1Last Post: 10-21-2010, 08:33 PM -
TreeSet Demonstration
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:34 PM -
ClassCastException in TreeSet
By pHew in forum New To JavaReplies: 2Last Post: 01-16-2008, 12:20 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks