Om Prakash
Om Prakash

Reputation: 2881

Count number of key in list of Map in Scala

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.

Upvotes: 0

Views: 768

Answers (1)

Thomas Böhm
Thomas Böhm

Reputation: 1486

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.

Upvotes: 1

Related Questions