• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
I was her plaything! And so was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic