如何将 Array[String] 转换为 Set[String]? [英] How do I convert an Array[String] to a Set[String]?

查看:64
本文介绍了如何将 Array[String] 转换为 Set[String]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串数组.将它变成一组不可变的字符串的最佳方法是什么?

I have an array of strings. What's the best way to turn it into an immutable set of strings?

我认为这是一个单一的方法调用,但我在 Scala 文档中找不到它.

I presume this is a single method call, but I can't find it in the scala docs.

我使用的是 Scala 2.8.1.

I'm using scala 2.8.1.

推荐答案

这个方法叫做toSet,例如:

scala> val arr = Array("a", "b", "c")
arr: Array[java.lang.String] = Array(a, b, c)

scala> arr.toSet
res1: scala.collection.immutable.Set[java.lang.String] = Set(a, b, c)

在这种情况下,Array 不存在 toSet 方法.但是有一个隐式转换到 ArrayOps.

In this case toSet method does not exist for the Array. But there is an implicit conversion to ArrayOps.

在这种情况下,我建议您查看 预定义.通常你应该在那里找到一些合适的隐式转换.在这种情况下将使用 genericArrayOps.genericWrapArray 也可以使用,但优先级较低.

In such cases I can advise you to look in Predef. Normally you should find some suitable implicit conversion there. genericArrayOps would be used in this case. genericWrapArray also can be used, but it has lower priority.

这篇关于如何将 Array[String] 转换为 Set[String]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆