I am trying to do the following and I am getting the exception:
Node new_node=new Node(label);
The total_nodes is of class TreeSet.
I have done this only once when my tree is empty.But in the second time I get the exception.If anyone can help I would be grateful.
easy - first time it works, cause first element doesn't have to be compared. second time it fails, because the trees compare method is called. so what you have to do is to make your objects comparable - how exactly can be read in the documentation (which would have saved you a lot of time -> so god **** start to use it!). it says:
Red-Black tree based implementation of the SortedMap interface....
...Note that the ordering maintained by a sorted map (whether or not an explicit comparator is provided) must be consistent with equals if this sorted map is to correctly implement the Map interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Map interface is defined in terms of the equals operation, but a map performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal. The behavior of a sorted map is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Map interface.
in the comparator interface document you find the following information:
A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as TreeSet or TreeMap).
and after 20 secs of googleing i found this: Objects that implement the Comparable interface can be inserted into sorted data structures in the API such as TreeMap.
So guess what you have to do?!? right... implement the comparable interface.
reading sometimes is faster than asking - especially when it comes to programming you should get used to it. hope i could help ;-)
By vikassheelgupta in forum Java
Last Post: 07-16-2005, 03:07 AM
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center