• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 342
  • Last Modified:

class HashSet

I insert some instances of class A to HashSet . When I try to access them with Iterator I get an instance of class Object and not of class A. How can I solve this problem?
0
Afifior
Asked:
Afifior
  • 2
1 Solution
 
CEHJCommented:
You must cast the result to Class A as in:
ClassA s = (ClassA)iterator.next()
0
 
Jim CakalicSenior Developer/ArchitectCommented:
Did you try simply downcasting the returned objects? Example:

    HashSet hash = new HashSet();
    hash.add(new A());
    Iterator iter = hash.iterator();
    while (iter.hasNext()) {
        A each = (A)iter.next();
    }

Your objects haven't changed type. It is simply that the collections are not type-specific so they accept and return all members as type Object. You can still downcast to the specific type safely -- assuming that your collections are homogeneous.

Best regards,
Jim Cakalic
0
 
CEHJCommented:
Hmm. I'm getting a feeling of deja vu!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now