aspose file tools*
The moose likes Java in General and the fly likes Reading Complex Map and Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reading Complex Map and Collection" Watch "Reading Complex Map and Collection" New topic
Author

Reading Complex Map and Collection

Rauf Khan
Greenhorn

Joined: Aug 11, 2010
Posts: 10
Am struck in a problem please let me know if you have any idea how to proceed on this.

I have a method myMethod(Map<String, Object> inputData) this Object can be a String/PrimitiveWrapper/Map/Collection i.e List/Vector. I need to get the values from this Object in the format given below.

If the Object is a Map. If it’s a simple Map its fine but this one is a complex Map. This Map can contain any type of data.
Suppose if it contains the data as shown below:



I need to iterate the eight Map i.e in backward direction and values should be read as below. The big question is how do I know what is there in the Object. As this Object can contain any data structure.

Description of the above values:
Customers is the key of eight map that contains seven
Customer is the key of seven that contains a list six so it should be index form Customer[0], Customer[1]
fiveA is part of six at index 0,
Addresses is the key of fiveA, that contains Map four
Address is the key of four that contains a list three, so it should be index for Address[0], Address[1].
AddressLine1 is the key of one and has value House 1
so the final value from the top looks like this:

Now I need to get the values in the above way and put the same in another map.


There will be no XML data as input, only it will be of the form myMethod(Map<String, Object> inputData) and this Object can be a String/PrimitiveWrapper/Map/Collection i.e List/Vector. Now the inputData I have to process is: myMethod(Map<String, Object> eight)

Thanks in Advance
Khan

prem pillai
Ranch Hand

Joined: Nov 02, 2007
Posts: 87

Welcome to JavaRanch



I would probably create a Customer class and Address class and then may be use "instanceof" operator, rather than what is shown in your code.
Rauf Khan
Greenhorn

Joined: Aug 11, 2010
Posts: 10
Pillai thanks for the reply.

But here I cant create any class, how will I know what is the input Map data. The values Customers, Customer, Addresses, .... are Map Key values. There might be something in java how to read such type of data. The one which I have shown is the sample. The same map can contain extra data also.



Output:

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Why don't you just create a Customer class, which contains fields like name and address; then you just return a List<Customer>?
Rauf Khan
Greenhorn

Joined: Aug 11, 2010
Posts: 10
Stephan thanks for the response.

I cannot create a Customer class. Am getting the data from the third party and I need to process through that data and get the values. I get the data in the format HashMap<String, Object> only. I have no idea what is present in the Map. I just need to iterate and get the data.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

How do you get the data? Is it sent to you through a socket connection or something? When you're done reading the data, what's going to happen to it? Are you going to process it yourself, does it get delegated to third party classes?

TellTheDetails please.
prem pillai
Ranch Hand

Joined: Nov 02, 2007
Posts: 87

Rauf Khan wrote:I cannot create a Customer class. Am getting the data from the third party


Rauf , for integrating your system with a thirdparty system, the first thing you need is a contract defining how these two systems are going communicate each other and what should be the structure of the data.. This is true even in case you are just using just thirdparty library- there the contract may just an API exposed by the thirdparty library... Are you saying that you don't this information with you ? If so thats not a very encouraging situation ...

Rauf Khan
Greenhorn

Joined: Aug 11, 2010
Posts: 10
Am developing a component which will be place in the application as a JAR. The application will call my method and pass input data in format (Map<String, Object> inputData)and i need to process this data.

I got the first break through to get the values but still couldnt get the output as expected. Can anyone please help me out to get the exact output.



Expected output:


Actual Output
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Rauf Khan wrote:and i need to process this data

How do you know how to process this data? Where is the contract?
 
Consider Paul's rocket mass heater.
 
subject: Reading Complex Map and Collection