在 Scala 中创建列表和集合:我实际上得到了什么?

发布于 2024-09-07 11:22:13 字数 832 浏览 4 评论 0原文

如果我使用 Set(1, 2, 3) 在 Scala 中创建一个 Set,我会得到一个 immutable.Set

scala> val s = Set(1, 2, 3)
s: scala.collection.immutable.Set[Int] = Set(1, 2, 3)

Q1:这到底是一个什么样的套装?它是一些哈希集吗?例如,查找的复杂性是多少?

问题2:在哪里可以阅读到这种“创建集合”的方法?我认为这是 apply 方法,但文档说“此方法允许将集合解释为谓词。它返回 true,当且仅当该集合包含元素 elem 时。


类似,如果我使用 List(1, 2, 3) 创建一个 List,我会得到

scala> val l = List(1, 2, 3)
l: List[Int] = List(1, 2, 3)

scala> l.getClass
res13: java.lang.Class[_] = class scala.$colon$colon

Q3: 再说一遍,我会得到什么?在这种情况下,我什至无法立即判断它是否可变,因为它甚至不是 scala.collection 包的一部分。为什么它存在于 scala 包中?

问题 4: 在 API 中,我可以在哪里阅读有关此“列表创建”方法的信息?

If I create a Set in Scala using Set(1, 2, 3) I get an immutable.Set.

scala> val s = Set(1, 2, 3)
s: scala.collection.immutable.Set[Int] = Set(1, 2, 3)

Q1: What kind of Set is this actually? Is it some hash-set? What is the complexity of look-ups for instance?

Q2: Where can I read up on this "set-creating" method? I thought that it was the apply method but the docs says "This method allows sets to be interpreted as predicates. It returns true, iff this set contains element elem."


Similarly, if I create a List using List(1, 2, 3), I get

scala> val l = List(1, 2, 3)
l: List[Int] = List(1, 2, 3)

scala> l.getClass
res13: java.lang.Class[_] = class scala.$colon$colon

Q3: Again, what do I get? In this case I can't even immediately tell if it's mutable or not, since it's not even part of the scala.collection-package. Why does this live in the scala package?

Q4: Where in the API can I read about this "list-creating" method?

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

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

发布评论

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

评论(3

好倦 2024-09-14 11:22:13

Q1:在这种特定情况下,您会得到一个 Set3,它是一个由三个参数组成的不可变集合。据推测它使用 if-else if-else 来检查包含。如果您创建的元素超过 4 个,您将获得一个不可变的哈希集。

Q2:你需要查看对象Set的apply方法,而不是类。 Set 类的 apply 方法是在执行 someSet(something) 时调用的方法。

Q3: scala.:: 是一个非空不可变单链表(如果执行不带参数的 List() 操作,则会得到 Nil 这是一个不可变空列表)。它位于 scala 包中,因为它被认为非常基本,因此属于基础包。

Q4:参见 Q2。

Q1: In this specific case you get a Set3 which is an immutable set of exactly three arguments. Presumably it uses if-else if-else to check inclusion. If you create a set of more than 4 elements, you get an immutable hash set.

Q2: You need to look at the apply method of the object Set, not the class. The apply method of the Set class is what is called when you do someSet(something).

Q3: scala.:: is a non-empty immutable singly linked list (if you do List() without arguments, you get Nil which is an immutable empty list). It lives in the scala package because it is considered so basic that it belongs in the base package.

Q4: See Q2.

一袭白衣梦中忆 2024-09-14 11:22:13

只是为了补充 sepp2k 对第三季度的出色回答,他说

它位于 scala 包中,因为
它被认为是如此基本以至于它
属于基础包。

这适用于 Scala 2.7

在 Scala 2.8 中,集合类已被重新组织,现在 :: 类位于 scala.collection.immutable 中,名称为 scala.:: 是 scala.collection.immutable.:: 的类型别名。

Welcome to Scala version 2.8.0.RC5 (OpenJDK 64-Bit Server VM, Java 1.6.0_18).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val l = List(1, 2, 3)
l: List[Int] = List(1, 2, 3)

scala> l.getClass
res0: java.lang.Class[_] = class scala.collection.immutable.$colon$colon

scala> scala.::
res1: collection.immutable.::.type = scala.collection.immutable.$colon$colon$@6ce5d622

Just to add to sepp2k's excellent answer to Q3, where he says

It lives in the scala package because
it is considered so basic that it
belongs in the base package.

This applies to Scala 2.7

In Scala 2.8, the collections classes have been reorganized, and now the :: class lives in scala.collection.immutable, and the name scala.:: is a type alias for scala.collection.immutable.::.

Welcome to Scala version 2.8.0.RC5 (OpenJDK 64-Bit Server VM, Java 1.6.0_18).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val l = List(1, 2, 3)
l: List[Int] = List(1, 2, 3)

scala> l.getClass
res0: java.lang.Class[_] = class scala.collection.immutable.$colon$colon

scala> scala.::
res1: collection.immutable.::.type = scala.collection.immutable.$colon$colon$@6ce5d622
耶耶耶 2024-09-14 11:22:13

如果你调用 getClass 方法,

scala> val list = List(1,2,3,45)
list: List[Int] = List(1, 2, 3, 45)

scala> val seq = Seq(1,2,3,4,5)
seq: Seq[Int] = List(1, 2, 3, 4, 5)

scala> list.getClass
res13: Class[_ <: List[Int]] = class scala.collection.immutable.$colon$colon

scala> seq.getClass
res14: Class[_ <: Seq[Int]] = class scala.collection.immutable.$colon$colon

那是因为 scala.collection.immutable.List 是一个抽象类,它有两个实现:scala.Nil 类和 scala.:: 。在 Scala 中,:: 是一个有效的标识符,您可以使用它来命名一个类。 Nil 代表空列表,scala.:: 代表任何非空列表。

if you call getClass method on the

scala> val list = List(1,2,3,45)
list: List[Int] = List(1, 2, 3, 45)

scala> val seq = Seq(1,2,3,4,5)
seq: Seq[Int] = List(1, 2, 3, 4, 5)

scala> list.getClass
res13: Class[_ <: List[Int]] = class scala.collection.immutable.$colon$colon

scala> seq.getClass
res14: Class[_ <: Seq[Int]] = class scala.collection.immutable.$colon$colon

That’s because scala.collection.immutable.List is an abstract class, and it comes with two implementations: the scala.Nil class and scala.::. In Scala, :: is a valid identifier, and you could use it to name a class. Nil represents an empty list, and scala.:: represents any nonempty list.

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