This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Iterating through a MAP that is in a different class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Iterating through a MAP that is in a different class." Watch "Iterating through a MAP that is in a different class." New topic
Author

Iterating through a MAP that is in a different class.

Joe Solis
Greenhorn

Joined: Jun 17, 2010
Posts: 5
I'm trying to iterate through a MAP that is in a different class than the one I'm doing the iterating in.

In class: Company, I have defined a Map:

private Map<String, Insured> insureds = new HashMap<String, Insured>();

This is a MAP of Insureds that in turn contains as SET of Coverages that in turn contains a SET of Products.

In class: CoverageReport, I am using this statement to try to iterate through the Insured MAP to get information of the Insureds, their Coverages and in turn their Products:

for(Map.Entry<String, Insured> entry : insureds.entrySet()) {
Insured insureds = entry.getValue();
InsRpt.append(entry.toString()).append("\n");
InsRpt.append("Coverage: ");


the interpreter is telling me that the variable: insureds cannot be resolved.

Can anyone tell me what I'm doing wrong?

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Welcome to JavaRanch.

insureds is a private member variable of your class Company. Because it is private, it's only visible inside that class. What you can do is add a getter method to class Company, and call that from class CoverageReport. But note that there you will need to have a Company object to call the method on. For example:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

I would like to make one modification to that:
That way the user can iterate through the map, and get values for keys, but not change the map in any way.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joe Solis
Greenhorn

Joined: Jun 17, 2010
Posts: 5
Thanks for the advice
Joe Solis
Greenhorn

Joined: Jun 17, 2010
Posts: 5
Continuing with this project, I am now trying to get information from a Class called Coverage.

As I mentioned earlier, this project uses a MAP of Insureds, which contains a SET of Coverages, which in turn contains a SET of Products. Inside Class: Coverage, there is a METHOD called: totalProductCost that iterates through the SET of products and accumulates the cost of the products for each coverage.

What I am trying to do in a Class called "CoverageReport" is to "Drill-down" into Class: Coverage to get at this Method.

In the Java class I'm taking, we learned how to access collection data on a single level, but not how to "drill-down" into multiple levels.


Thanks.


Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
Any chance for SSCCE? I see so much text with so little info. TellTheDetails! How would you describe 'drill-down'? How is your class CoverageReport related to Coverage ... is that where you're maintaining a Map of Coverage objects?


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Iterating through a MAP that is in a different class.