We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,894 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to find if any map value has a certain attribute v

alue
I have a Customer class and a Repair class
I have a TreeMap(customerDetails) of type <String, Customer>.
Each Customer object holds a HashMap(repairs) of type<String, Repair>, the String represents the ref number of the repair.
The user of my system types in a ref number, the program searches to see if any Customer has any Repair with this ref number as a value the displays the Customers details and the Repair details for that ref number.

I'm not sure how to go about searching the customerDetails Map and the repair Map to find the Repair mapped to to String refNumber.
Any help appreciated.
Thanks

4
Contributors
14
Replies
1 Week
Discussion Span
10 Months Ago
Last Updated
15
Views
Question
Answered
GlenRogers
Posting Whiz in Training
256 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Loop thru coustomers, loop thru each customer's repairs looking for the right key value, eg

for (Customer c : customers) {
    for (String repairNo  : c.getRepairs().keySet()) {
       if (repairNo.equals(...)) ... Repair r = c.getRepairs().get(repairNo)

(this code can be optimised, but that's the clearest version to understand)

JamesCherrill
... trying to help
Moderator
8,512 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30
for(Customer cus : map)

this line gives me thi error,
for each not applicable to expression type
required; array or java.lang.Iterable
found java.util.Map"

GlenRogers
Posting Whiz in Training
256 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Is map a map, list, collection, array filled with (only) objects of the type Customer?

stultuske
Industrious Poster
4,371 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

map is a Map.................

GlenRogers
Posting Whiz in Training
256 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

a Map exists out of an Object and a Key, you are treating it as a list or array which has only the Object as element.
that's your problem.

stultuske
Industrious Poster
4,371 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

I was illustrating the way to solve your problem, not giving actual complete code. You just need to loop through all your Customers, and given your Map you will find those in map.values()
ps When I used ... on line 3, that's not real Java syntax :-)

JamesCherrill
... trying to help
Moderator
8,512 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

Ok, so now at this line

for(Customer c : aMap.values())

I get this error

"Incompatible types
required: rbdeskapp.Customer
found: java.lang.Object"

aMaps values are Customer objects

Can anyone shed some light?
Thanks

GlenRogers
Posting Whiz in Training
256 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Can you post the definition for the aMap variable.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

I have read a TreeeMap to a file in another method, then read that file back it this methon, casting what I read back to a TreeMap

File file = new File("reboot_customer_details.txt");  
FileInputStream f = new FileInputStream(file);  
ObjectInputStream s = new ObjectInputStream(f);  
aMap = (TreeMap)s.readObject();
s.close();
GlenRogers
Posting Whiz in Training
256 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Sorry I forgot to show you this.
I created a Map in the methood like this

Map aMap = new TreeMap<String, Customer>();
GlenRogers
Posting Whiz in Training
256 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Can you post the definition of aMap.
A definition has the datatype followed by the variable name. The first 3 lines of your post define the variables: file, f and s. aMap is not defined, It is assigned a value.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Is this what you mean?

Map aMap = new TreeMap<String, Customer>();
GlenRogers
Posting Whiz in Training
256 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

The definition of aMap (Map aMap) is without any type and defaults to Object. That's why the compiler says:

found: java.lang.Object"

Give it a definition:
Map<K, V> aMap

with the correct types for K and V

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Forgot to update this.
Solved, thanks for all your help gys.......

GlenRogers
Posting Whiz in Training
256 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 10 Months Ago by NormR1, stultuske and JamesCherrill

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1085 seconds using 2.7MB