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.

Community
  • 1
  • 1
Om Prakash
  • 2,675
  • 4
  • 29
  • 50
  • If you know the list has only one map, why don't you get the list's head? – Cyrille Corpet Apr 14 '17 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 :) – Om Prakash Apr 14 '17 at 10:10
  • `list.head.size` works like charm. :) – Om Prakash Apr 14 '17 at 10:10
  • May I know the reason from dear down-voter, why you down-voted my question? – Om Prakash Apr 14 '17 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. – halfer Apr 14 '17 at 13:55

1 Answers1

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.

Thomas Böhm
  • 1,456
  • 1
  • 15
  • 27