• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
hr rick
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
hr rick
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Henry, you really solved my problem. now i'll go find the codes behinds the TreeSet to really solve it. Thank you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic