You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by David Stevenson <ds...@rochester.rr.com> on 2004/12/02 03:50:37 UTC

[Collections] TreeBag java.lang.ClassCastException

commons-collections-2.1.1-src

I am a newbie commons_collections user, trying to learn the TreeBag Java
class. I am getting the following exception, and I assume I don't
understand something important. I have attached the code.
Maybe the underlying TreeMap doesn't get the Comparator set unless
you use the TreeBag ( Comparator c ) constructor? 
If you use the TreeBag ( Collections c ) or TreeBag () constructor,
maybe the Comparator in the underlying TreeMap is still null?
Or when you use TreeBag ( Collection c ) constructor, it looks for
the Comparator implementation on the Collection class (i.e. ArrayList?).
Would I have to extend the ArrayList Java class and implement the
Comparator interface for the TreeBag ( Collection c ) interface to 
work?

Exception in thread "main" java.lang.ClassCastException
        at java.util.TreeMap.compare(TreeMap.java:1085)
        at java.util.TreeMap.getEntry(TreeMap.java:345)
        at java.util.TreeMap.get(TreeMap.java:264)
        at org.apache.commons.collections.MapUtils.getNumber(Unknown
Source)
        at org.apache.commons.collections.MapUtils.getInteger(Unknown
Source)
        at org.apache.commons.collections.DefaultMapBag.getCount(Unknown
Source)
        at org.apache.commons.collections.DefaultMapBag.add(Unknown
Source)
        at org.apache.commons.collections.DefaultMapBag.add(Unknown
Source)
        at org.apache.commons.collections.DefaultMapBag.addAll(Unknown
Source)
        at TreeBagTest.main(TreeBagTest.java:23)

Line 15: ArrayList userList = PhoenixUser.getTestData () ;
// Returns an ArrayList of PhoenixUser objects
// PhoenixUser implements Comparator interface, 
// public int hashCode ()
// public boolean equals ( Object object ), 
// public int compare ( Object object1, Object object2 )

Line 22: phoenixUserTreeBag = new TreeBag () ;
Line 23: phoenixUserTreeBag.addAll ( userList ) ;


The following code works:
Line 15: TreeBag phoenixUserTreeBag = new TreeBag ( new PhoenixUser (
"", "" ) ) ;
Line 16: phoenixUserTreeBag.addAll ( userList ) ;
Line 17: System.out.println ( "hashBag: " + phoenixUserTreeBag.toString
() ) ;

Line 19: Set phoenixUserSet = phoenixUserTreeBag.uniqueSet () ;
Line 20System.out.println ( "phoenixUserSet: " + phoenixUserSet.toString
() ) ;

Lines 15-19 gives the following output:

hashBag: [1:{ phoenix_user user_id="u1" user_name="user1" },1:{
phoenix_user user_id="u2" user_name="user2" },1:{ phoenix_user
user_id="u3" user_name="user3" },1:{ phoenix_user user_id="u4"
user_name="user4" }]
phoenixUserSet: [{ phoenix_user user_id="u1" user_name="user1" }, {
phoenix_user user_id="u2" user_name="user2" }, { phoenix_user
user_id="u3" user_name="user3" }, { phoenix_user user_id="u4"
user_name="user4" }]

David Stevenson




Re: [Collections] TreeBag java.lang.ClassCastException

Posted by David Stevenson <ds...@rochester.rr.com>.
I fixed it by implementing the Comparable interface in the PhoenixUser
Java class. Then TreeBag () and TreeBag ( Collection c ) constructors
work fine with the TreeBag.addAll ( Collection c ) method.

David Stevenson

On Wed, 2004-12-01 at 21:50, David Stevenson wrote:
> commons-collections-2.1.1-src
> 
> I am a newbie commons_collections user, trying to learn the TreeBag Java
> class. I am getting the following exception, and I assume I don't
> understand something important. I have attached the code.
> Maybe the underlying TreeMap doesn't get the Comparator set unless
> you use the TreeBag ( Comparator c ) constructor? 
> If you use the TreeBag ( Collections c ) or TreeBag () constructor,
> maybe the Comparator in the underlying TreeMap is still null?
> Or when you use TreeBag ( Collection c ) constructor, it looks for
> the Comparator implementation on the Collection class (i.e. ArrayList?).
> Would I have to extend the ArrayList Java class and implement the
> Comparator interface for the TreeBag ( Collection c ) interface to 
> work?
> 
> Exception in thread "main" java.lang.ClassCastException
>         at java.util.TreeMap.compare(TreeMap.java:1085)
>         at java.util.TreeMap.getEntry(TreeMap.java:345)
>         at java.util.TreeMap.get(TreeMap.java:264)
>         at org.apache.commons.collections.MapUtils.getNumber(Unknown
> Source)
>         at org.apache.commons.collections.MapUtils.getInteger(Unknown
> Source)
>         at org.apache.commons.collections.DefaultMapBag.getCount(Unknown
> Source)
>         at org.apache.commons.collections.DefaultMapBag.add(Unknown
> Source)
>         at org.apache.commons.collections.DefaultMapBag.add(Unknown
> Source)
>         at org.apache.commons.collections.DefaultMapBag.addAll(Unknown
> Source)
>         at TreeBagTest.main(TreeBagTest.java:23)
> 
> Line 15: ArrayList userList = PhoenixUser.getTestData () ;
> // Returns an ArrayList of PhoenixUser objects
> // PhoenixUser implements Comparator interface, 
> // public int hashCode ()
> // public boolean equals ( Object object ), 
> // public int compare ( Object object1, Object object2 )
> 
> Line 22: phoenixUserTreeBag = new TreeBag () ;
> Line 23: phoenixUserTreeBag.addAll ( userList ) ;
> 
> 
> The following code works:
> Line 15: TreeBag phoenixUserTreeBag = new TreeBag ( new PhoenixUser (
> "", "" ) ) ;
> Line 16: phoenixUserTreeBag.addAll ( userList ) ;
> Line 17: System.out.println ( "hashBag: " + phoenixUserTreeBag.toString
> () ) ;
> 
> Line 19: Set phoenixUserSet = phoenixUserTreeBag.uniqueSet () ;
> Line 20System.out.println ( "phoenixUserSet: " + phoenixUserSet.toString
> () ) ;
> 
> Lines 15-19 gives the following output:
> 
> hashBag: [1:{ phoenix_user user_id="u1" user_name="user1" },1:{
> phoenix_user user_id="u2" user_name="user2" },1:{ phoenix_user
> user_id="u3" user_name="user3" },1:{ phoenix_user user_id="u4"
> user_name="user4" }]
> phoenixUserSet: [{ phoenix_user user_id="u1" user_name="user1" }, {
> phoenix_user user_id="u2" user_name="user2" }, { phoenix_user
> user_id="u3" user_name="user3" }, { phoenix_user user_id="u4"
> user_name="user4" }]
> 
> David Stevenson
> 
> 
> 
> 
> ______________________________________________________________________
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org