File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes TreeSet contains method doesn't work for me?????? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "TreeSet contains method doesn Watch "TreeSet contains method doesn New topic
Author

TreeSet contains method doesn't work for me??????

hr rick
Greenhorn

Joined: Jul 10, 2011
Posts: 6
I want to put the custom's data into a TreeSet. When the custom number is same, I add the volume of trade.
Here is my TradeNode class that implements the Comparable Interator.


and the test class is :

I supposed the output shoud be like this :

but the output is

Could someone help me and point me out where my fault is. Thanks a lot!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39


Interesting idea. You don't know the total amount of trade, so you'll merge (add them up) them at sort time by adding them when a comparison is equal. Your compareTo() method is responsible for sort order -- which you decided to use the trade value (the amount to trade) itself.

hr rick wrote:
Could someone help me and point me out where my fault is. Thanks a lot!


1. The compareTo() method is used by the sorting algorithm to sort the element into the correct order. In order to do this, this method may be called many times during an insert -- not guaranteed to be called only once.

2. You are sorting on trade value, but this value is changing during the sort. In effect, the ordering has to change as the items are being inserted -- but the TreeSet class doesn't know this, and hence, the data structure will be different (ie. wrong and/or corrupted) as items are added.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
hr rick
Greenhorn

Joined: Jul 10, 2011
Posts: 6
thank you, Henry.
I tested it in another Testclass.

the output is :

I think I got something in your answer, and I think if i make it sync, would it work right? Thanks.
If doesn't , would you please tell me how can i make it?
sorry for my poor english
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

hr rick wrote:
I think I got something in your answer, and I think if i make it sync, would it work right? Thanks.
If doesn't , would you please tell me how can i make it?


I think that totaling the trade data while inserting it into the TreeSet is not a good idea. Do those two tasks separately.... And if you don't want to do it separately, do if differently. Sort it differently that doesn't use trade count. Prior to add, check if the item is already in the list, and replace it with the new trade count. Do this logic with the add, and not in the compareTo() method.

Henry

hr rick
Greenhorn

Joined: Jul 10, 2011
Posts: 6
Ok, Henry. I updated my codes according to your idea.
Here is my update compareTo method:



and the output still like this:


I think it should be like this:


Where is my fault???
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

hr rick wrote:
Where is my fault???


Same answer... the compareTo() method is used by the sorting algorithm to sort the element into the correct order. In order to do this, this method may be called many times during an insert -- not guaranteed to be called only once.

However, I guess I should add that there is also no reason to compare to every element either. For example, if the second B is compared to the A, and is determined to be less than that A, there is no reason to compare it to the earlier B that was compared to be more than that A. After all, how can two B's, one greater than A, the other less than A, be possibility equal?

Henry

hr rick
Greenhorn

Joined: Jul 10, 2011
Posts: 6
Thanks, Henry, you really solved my problem. now i'll go find the codes behinds the TreeSet to really solve it. Thank you!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TreeSet contains method doesn't work for me??????
 
Similar Threads
Doubt about Trees Implementations
how do you remove a node from a binary tree?
hashCode() & equals() method test
Comparator doubt
Binary tree