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.

4 Years
Discussion Span
Last Post by GlenRogers

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)

Edited by JamesCherrill

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"


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.


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 :-)


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?

Edited by GlenRogers


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();

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

Map aMap = new TreeMap<String, Customer>();

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.


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

Edited by NormR1

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.