Link to home
Start Free TrialLog in
Avatar of hzinox
hzinox

asked on

ClassCastException when casting a value into a Set

Hi,

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) iter.next();
           centroidSet.add(sortedEntries.getValue());
      }
     
Pixel pixel = null;
int sumRed = 0, sumGreen = 0, sumBlue = 0;

Iterator it = centroidSet.iterator();
while (it.hasNext()) {
     pixel = (Pixel)it.next(); --------------> 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.
Thanks
Avatar of Mayank S
Mayank S
Flag of India image

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

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

to see what it is (it would be same as what sortedEntries.getValue () returns)
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> 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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why sarcastic? I think its a good idea. It also improves performance
Avatar of fffej78
fffej78

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?" :)
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 ;-)
Avatar of hzinox

ASKER

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 ;-)...