在 Scala 中将 Map 展平为 varargs 字符串列表

发布于 2024-12-17 22:27:32 字数 563 浏览 0 评论 0原文

Java 事件日志记录(分析)库有一个方法,该方法采用事件名称,然后是可变参数字符串列表,如下所示:

Analytics.event("my event name", "key1", "value1", "key2", "value2" ...)

我已将事件参数收集到像这样的 Map 中

Map("key1" -> "value1", "key2" -> "value2" ...)

,现在必须有一种方法来展平 Map 列出键和值交替的位置,然后将其提供给事件方法。我有几个猜测,比如将 Map 转换为列出 TupleList,但调用 .flatten 会显示“

No implicit view available from (java.lang.String, java.lang.String) => scala.collection.TraversableOnce[B]

我是什么”这里不见了?

A Java event logging (analytics) library has a method that takes an event name and then varargs list of Strings like this:

Analytics.event("my event name", "key1", "value1", "key2", "value2" ...)

I've collected my event parameters in to a Map like

Map("key1" -> "value1", "key2" -> "value2" ...)

Now there must be a way to flatten the Map to list where keys and values alternate and then feed it to the event method. I've had several guesses, like transforming the Map to list a List of Tuples, but calling .flatten on that says

No implicit view available from (java.lang.String, java.lang.String) => scala.collection.TraversableOnce[B]

What am I missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤云独去闲 2024-12-24 22:27:33
val m = Map ("key1" -> "value1", "key2" -> "value2", "k3"-> "v3")
val li = m.map ((e)=> List (e._1, e._2)).flatten.toSeq 
event ("name", li: _*)
val m = Map ("key1" -> "value1", "key2" -> "value2", "k3"-> "v3")
val li = m.map ((e)=> List (e._1, e._2)).flatten.toSeq 
event ("name", li: _*)
温柔一刀 2024-12-24 22:27:32

您可以使用 : _* 运算符:

def event(name: String, values: String*) {/*...*/}

val params = Map("key1" -> "value1", "key2" -> "value2")
val paramsArr = params flatMap {case(k, v) => List(k, v)} toArray

event("name", paramsArr: _*)

不确定这是否也适用于 Java 可变参数(请告诉我们!)

You can use the : _* operator:

def event(name: String, values: String*) {/*...*/}

val params = Map("key1" -> "value1", "key2" -> "value2")
val paramsArr = params flatMap {case(k, v) => List(k, v)} toArray

event("name", paramsArr: _*)

Not sure whether this works with Java varargs as well (let us know!)

纵性 2024-12-24 22:27:32

您可以使用 flatMap 将每个键/值对映射到包含两个元素的列表,然后连接这些列表。

scala> Map("k1" -> "v1", "k2" -> "v2")
res0: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map(k1 -> v1, k2 -> v2)

scala> res0.flatMap { case (k,v) => k::v::Nil }
res1: scala.collection.immutable.Iterable[java.lang.String] = List(k1, v1, k2, v2)

一旦你有了这个,你就可以将列表传递给事件方法:

Analytics.event("my event name", res1:_*)

You can use flatMap to map every key/value pair to a list with two elements and then concatenate these lists.

scala> Map("k1" -> "v1", "k2" -> "v2")
res0: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map(k1 -> v1, k2 -> v2)

scala> res0.flatMap { case (k,v) => k::v::Nil }
res1: scala.collection.immutable.Iterable[java.lang.String] = List(k1, v1, k2, v2)

Once you have that, you can just pass the list to the event method:

Analytics.event("my event name", res1:_*)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文