• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Iterating through a MAP that is in a different class.

 
Joe Solis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15627
46
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Rob Spoor
Sheriff
Pie
Posts: 20749
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Solis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice
 
Joe Solis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic