Solved

class HashSet

Posted on 2002-04-15
3
331 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 77
@SBGen Method 3 35
How to monitor the number of connections/threads being utilized on Tomcat 6.0 8 50
expectj telnet failing 5 17
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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 …

937 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

4 Experts available now in Live!

Get 1:1 Help Now