Questions tagged [scala-collections]

Collection library for Scala Programming Language

Related Tags:

1639 questions
873
votes
18 answers

Is the Scala 2.8 collections library a case of "the longest suicide note in history"?

I have just started to look at the Scala collections library re-implementation which is coming in the imminent 2.8 release. Those familiar with the library from 2.7 will notice that the library, from a usage perspective, has changed little. For…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
232
votes
4 answers

Scala 2.8 breakOut

In Scala 2.8, there is an object in scala.collection.package.scala: def breakOut[From, T, To](implicit b : CanBuildFrom[Nothing, T, To]) = new CanBuildFrom[From, T, To] { def apply(from: From) = b.apply() ; def apply() = b.apply() } I…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
217
votes
6 answers

When should I choose Vector in Scala?

It seems that Vector was late to the Scala collections party, and all the influential blog posts had already left. In Java ArrayList is the default collection - I might use LinkedList but only when I've thought through an algorithm and care enough…
Duncan McGregor
  • 17,665
  • 12
  • 64
  • 118
208
votes
4 answers

What is the difference between JavaConverters and JavaConversions in Scala?

In scala.collection, there are two very similar objects JavaConversions and JavaConverters. What is the difference between these two objects? Why do they both exist? When do I want to use one vs. the other?
Michael Ekstrand
  • 28,379
  • 9
  • 61
  • 93
202
votes
3 answers

Reduce, fold or scan (Left/Right)?

When should I use reduceLeft, reduceRight, foldLeft, foldRight, scanLeft or scanRight? I want an intuition/overview of their differences - possibly with some simple examples.
Marc Grue
  • 5,865
  • 3
  • 16
  • 23
178
votes
13 answers

Scala best way of turning a Collection into a Map-by-key?

If I have a collection c of type T and there is a property p on T (of type P, say), what is the best way to do a map-by-extracting-key? val c: Collection[T] val m: Map[P, T] One way is the following: m = new HashMap[P, T] c foreach { t => m add…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
160
votes
3 answers

Difference between Array and List in scala

In what cases I should use Array(Buffer) and List(Buffer). Only one difference that I know is that arrays are nonvariant and lists are covariant. But what about performance and some other characteristics?
Jeriho
  • 7,129
  • 9
  • 41
  • 57
139
votes
1 answer

Stream vs Views vs Iterators

What are the differences among Streams, Views (SeqView), and Iterators in scala? This is my understanding: They are all lazy lists. Streams cache the values. Iterators can only be used once? You can't go back to the beginning and evaluate the value…
JWC
  • 1,745
  • 2
  • 12
  • 14
129
votes
7 answers

How to read files from resources folder in Scala?

I have a folder structure like below: - main -- java -- resources -- scalaresources --- commandFiles and in that folders I have my files that I have to read. Here is the code: def readData(runtype: String, snmphost: String, comstring: String,…
Pradip Karad
  • 1,377
  • 2
  • 9
  • 8
122
votes
3 answers

What does param: _* mean in Scala?

Being new to Scala (2.9.1), I have a List[Event] and would like to copy it into a Queue[Event], but the following Syntax yields a Queue[List[Event]] instead: val eventQueue = Queue(events) For some reason, the following works: val eventQueue =…
Chris
  • 2,057
  • 2
  • 16
  • 20
107
votes
4 answers

Scala: What is the difference between Traversable and Iterable traits in Scala collections?

I have looked at this question but still don't understand the difference between Iterable and Traversable traits. Can someone explain ?
Rahul
  • 12,886
  • 13
  • 57
  • 62
106
votes
10 answers

Elegant way to invert a map in Scala

Learning Scala currently and needed to invert a Map to do some inverted value->key lookups. I was looking for a simple way to do this, but came up with only: (Map() ++ origMap.map(kvp=>(kvp._2->kvp._1))) Anybody have a more elegant approach?
AlexeyMK
  • 6,245
  • 9
  • 36
  • 41
106
votes
3 answers

What is the correct way to get a subarray in Scala?

I am trying to get a subarray in scala, and I am a little confused on what the proper way of doing it is. What I would like the most would be something like how you can do it in python: x = [3, 2, 1] x[0:2] but I am fairly certain you cannot do…
nnythm
  • 3,280
  • 4
  • 26
  • 36
100
votes
3 answers

Why is Scala's immutable Set not covariant in its type?

EDIT: Re-written this question based on original answer The scala.collection.immutable.Set class is not covariant in its type parameter. Why is this? import scala.collection.immutable._ def foo(s: Set[CharSequence]): Unit = { println(s) } def…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
90
votes
10 answers

Converting a Java collection into a Scala collection

Related to Stack Overflow question Scala equivalent of new HashSet(Collection) , how do I convert a Java collection (java.util.List say) into a Scala collection List? I am actually trying to convert a Java API call to Spring's SimpleJdbcTemplate,…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
1
2 3
99 100