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.
public HuffKnoop makeHuffmanTree(TreeSet<HuffKnoop> tSH)
// Making huffman tree
while (tSH.size() > 1)
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);
HuffKnoop links = tSH.first();
HuffKnoop rechts = tSH.first();
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);
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?