Solved

class HashSet

Posted on 2002-04-15
3
334 Views
Last Modified: 2010-03-31
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
Comment
Question by:Afifior
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 6942595
You must cast the result to Class A as in:
ClassA s = (ClassA)iterator.next()
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 50 total points
ID: 6942600
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 6942655
Hmm. I'm getting a feeling of deja vu!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
program arguments vs VM arguments 4 56
What browser will run Java? 7 171
Delete .class files at every compile 4 57
convert Absolute path to relative path of FTP location 7 49
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

737 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