0

I have a list of Map:

List(Map(148e0b9b-e142-493f-a298-27e0ebd453bc -> 12, 15ddf513-44aa-4285-82cb-31017da99a64 -> 18, ce760bd7-0c2c-4f0f-9303-1ba77346694c -> 3)).

Here, every list contains only one map. I want to count how many key-value pair are there in the list of Map. Though it can be done by iterating and incrementing counter, but I am looking for something like one-liner.

This question is bit closer to my question, but I don't know how to unpack map from list of Map.

Expected output: 3.

5
  • If you know the list has only one map, why don't you get the list's head? Commented Apr 14, 2017 at 10:07
  • Oh! so cool. I didn't had idea. I am noob in scala. I would have saved my one hour, if I had posted one hour before. @CyrilleCorpet, thanks a ton :) Commented Apr 14, 2017 at 10:10
  • list.head.size works like charm. :) Commented Apr 14, 2017 at 10:10
  • May I know the reason from dear down-voter, why you down-voted my question? Commented Apr 14, 2017 at 10:18
  • ^ Hi Om. It's fine to enquire about voting decisions in the comments if you wish, though I personally think it's a waste of time. However, please don't edit voting advice or commentary into posts - most readers don't sign in or vote, so it is not of interest to them. Thanks. Commented Apr 14, 2017 at 13:55

1 Answer 1

1

A Version, which works, and also works for a list with multiple maps:

list.map{x => x.size}.sum

It gets the number of map-elements for each list-element, and sums up the result.

Sign up to request clarification or add additional context in comments.

Comments

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.