1

I have an array like this(The actual array is dynamic with 40values): ['t1',1, 't2', 3]

I need to convert the above to a hashmap in scala which looks like this: {'t1' => 1, 't2' => 2}

How can I achieve this. Any help would be much appreciated. Thank you.

1 Answer 1

2

This should work. It creates pairs of adjacent entries in array and creates a map for those pairs.

val arr = Array("t1",1, "t2", 3) 
arr.grouped(2).map(a => a(0) -> a(1)).toMap  
Sign up to request clarification or add additional context in comments.

2 Comments

when I try printing what you have given, it prints 'non-empty iterator'. Any idea why? I pretty new to scala.
sorry typo, fixed it. check again

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.