Navigation
Synopsis Convert a list of pairs to a map; first elements are associated with a set of second elements.
Function map[&A,set[&B]] toMap(list[tuple[&A, &B]] lst) throws MultipleKey
Usage import List;
Description Convert a list of tuples to a map in which the first element of each tuple is associated with the set of second elements from all tuples with the same first element. Keys should be unique.
Examples
rascal>import List;
ok
rascal>toMap([<1,10>, <1, 11>, <2, 20>, <3, 30>, <3, 31>]);
map[int, set[int]]: (
  1:{10,11},
  2:{20},
  3:{30,31}
)
Pitfalls toMap collects all values in tuples with the same first value in a set. Contrast this with toMapUnique that associates each first tuple value with the second tuple value, but imposes the constraint that those keys are unique.

Questions
Question [1].
The type of toMap([<9, 13>, <-18, 4>, <-11, -20>, <-2, 13>, <-19, -5>, <-7, 7>, <8, 15>, <-18, -10>]) is

Question [2].
toMap([<14, 7>, <-16, 9>, <8, -6>, <5, -9>, <0, 9>, <-7, 9>]) == 



Is this page unclear, or have you spotted an error? Please add a comment below and help us to improve it. For all other questions and remarks, visit ask.rascal-mpl.org.