开发者

Creating lists and sets in Scala: What do I actually get?

开发者 https://www.devze.com 2023-01-05 10:00 出处:网络
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)

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 g开发者_如何学JAVAet? 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?


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号