ClassCastException when casting a value into a Set

Posted on 2006-05-31
Last Modified: 2010-03-31

Could anyone help me out. I have a TreeMap which is filled with ArrayList which in turn filled with Pixel objects. Now I want to extract the Pixel object and put them in a set probably so that I can access each pixel r,g and b values. How can this be done? I have tried but always been given with a ClassCast Exception error :(. One way I have tried to do it is as below

Set centroidSet = new HashSet();
Set entry = clusterMap.entrySet();
for (int j = 0; j < clusterMap.size(); j++) {
      Iterator iter = entry.iterator();
      while (iter.hasNext()) {
           Map.Entry sortedEntries = (Map.Entry);
Pixel pixel = null;
int sumRed = 0, sumGreen = 0, sumBlue = 0;

Iterator it = centroidSet.iterator();
while (it.hasNext()) {
     pixel = (Pixel); --------------> here where the exception happened!
     sumRed += pixel.getRed();

I know why I got the exception. It's because I try to cast the sortedEntries.getValue (which is an ArrayList of Pixel objects) to a Pixel type in centroidSet. But don't know what other way I can try to solve it. Any help is very much appreciated.
Question by:hzinox
    LVL 30

    Expert Comment

    That's because its not a Pixel object. Try printing:

    System.out.println ( ().getClass () ) ;

    to see what it is (it would be same as what sortedEntries.getValue () returns)
    LVL 30

    Accepted Solution

    Well you need:

    ArrayList list = ( ArrayList ) () ;

    Now iterate through it.

    for ( int i = 0, count = list.size () ; i < count ; i ++ )
      Pixel p = ( Pixel ) list.get ( i ) ;
      // process 'p'
    LVL 2

    Assisted Solution

    Cast sortedEntries.getValue() to ArrayList, iterate over that list and add every pixel within it to the centroidSet.
    LVL 30

    Expert Comment

    >> Cast sortedEntries.getValue() to ArrayList, iterate over that list

    That is exactly what I proposed above.

    >> add every pixel within it to the centroidSet

    It is already in centroidSet, in an array-list.
    LVL 4

    Assisted Solution

    Slightly sarcastic answer perhaps, but Learn Java generics, use Java 5 and catch these at compile-time :)


    Even if you are stuck using Java 1.4 I recommend adding comments such that it is clear what is what in a collection type
    LVL 30

    Expert Comment

    Why sarcastic? I think its a good idea. It also improves performance
    LVL 4

    Expert Comment

    Well, perhaps sarcastic as it doesn't answer the question properly :)  I'm fairly new here and it strikes me that nine times out of ten, people are actually asking the wrong question to solve their problem, and this is probably one of these instances as it concentrates too much on the details, and not enough on the real problem

    The question should be "How can I ensure that Java collections have in what I expected?" :)
    LVL 30

    Expert Comment

    Yes, its also about knowing that you have to hold it in the correct reference so you have to know what you put in it ;-)

    Author Comment

    Hi everyone,

    Basically each one of you did gave me the idea to solve my problem. Done it and got it already. I will try to be fair in giving out the points ok ;-)...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    VB Script 3 60
    array220 challenge 8 28
    count11 challenge 6 30
    firstChar challenge 13 53
    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now