我有一个集合列表,并想向其中添加一个集合。我的假设是我必须使用::
operator添加到列表中。
我的代码:
// attribute_sets is a tuple (String, Set[String])
val listsOfSets: List[Set[String]] = attribute_sets
.foldLeft(List[Set[String]])((acc, attribute_set) => {
acc :: (attribute_set._2 + attribute_set._1)
})
IntelliJ 告诉我存在类型不匹配 ( expected String, actual Set[String]
),当我尝试添加到acc
.
您可以使用::
only 在列表的开头添加元素:
x :: list
要在列表末尾添加内容,您必须使用:+
运算符:
list :+ x
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句