commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Stevenson <dstev...@rochester.rr.com>
Subject [Collections] TreeBag java.lang.ClassCastException
Date Thu, 02 Dec 2004 02:50:37 GMT
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




Mime
View raw message