[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


ClassCastException when casting a value into a Set

Posted on 2006-05-31
Medium Priority
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) iter.next();
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.
Question by:hzinox
LVL 30

Expert Comment

by:Mayank S
ID: 16799464
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)
LVL 30

Accepted Solution

Mayank S earned 1400 total points
ID: 16799498
Well you need:

ArrayList list = ( ArrayList ) it.next () ;

Now iterate through it.

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

Assisted Solution

ms_blaster earned 400 total points
ID: 16805660
Cast sortedEntries.getValue() to ArrayList, iterate over that list and add every pixel within it to the centroidSet.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 30

Expert Comment

by:Mayank S
ID: 16805891
>> 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.

Assisted Solution

fffej78 earned 200 total points
ID: 16824156
Slightly sarcastic answer perhaps, but Learn Java generics, use Java 5 and catch these at compile-time :)

See http://java.sun.com/docs/books/tutorial/extra/generics/index.html

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

by:Mayank S
ID: 16826842
Why sarcastic? I think its a good idea. It also improves performance

Expert Comment

ID: 16826990
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

by:Mayank S
ID: 16830392
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

ID: 16863789
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 ;-)...

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

873 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