0

Lets say, I am fetching the data from excel and I need to map two things.

ID and the corresponding Dates, Excel data look like this :-

       ID                             Dates
      N1#N1          2018-10-09,2018-10-10#2018-10-11

Actual output should look like this :-

{N1=2018-10-09,2018-10-10} {N1=2018-10-11} 

I have tried below code:-

//Fetching from using in soap ui    
String id = context.expand('${Data#ID}')
String dt = context.expand('${Data#Dates}')

List arrId = id.split('#')

def strD

LinkedHashMap < String, String > dateMap= new LinkedHashMap < 
String, String > ()
for(int i=0; i<arrId.size(); i++) {
strD = dt.split("#").asType(List)[i]

dateMap.put(arrId[i],strD)

}

log.info dateMap

Could anyone help me on this?

6
  • what language is that? it doesn't even come close to valid java syntax Commented Jan 24, 2018 at 11:11
  • Your code is valid/working to me. Are you facing some execution error? Can you provide more details? Commented Jan 24, 2018 at 11:37
  • Ok, just give ID as N1 only. like this:- N1#N1 .. Still is it working? @Edumelzer Commented Jan 24, 2018 at 11:43
  • Hey, this is groovy actually.. @Stultuske Commented Jan 24, 2018 at 11:44
  • @avidCoder Ok, now I get a Map with just N1 and a single date record (like this: ([N1:2018-10-11]). In this case do you want the dates to be merged? Commented Jan 24, 2018 at 11:53

1 Answer 1

1

Try this:

def ids = "N1#N1".split('#')
def dts = "2018-10-09,2018-10-10#2018-10-11".split('#')

List<Map> result = (0..ids.size()-1).collect{
   [(ids[it]): dts[it]]
}

assert result == [[N1:"2018-10-09,2018-10-10"], [N1:"2018-10-11"]]
Sign up to request clarification or add additional context in comments.

6 Comments

hey, this is fine.. my code works with my code only.. I need ids to be N1#N1.. I have edited the questions. (Typo error)
Another try I gave is, by importing org.apache.commons.collections.map.MultiValueMap; And assigning inside dataMap.. But here, result is like this - {N1=[2018-10-09,2018-10-10, 2018-10-11]}
Map always have unique keys. Probably you need list of maps ? Updated answer
Thanks.. it worked, Can my code be converted the same way?
Yes, but you need to change it a little
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.