Questions tagged [scala-collections]

Collection library for Scala Programming Language

Related Tags:

1639 questions
21
votes
2 answers

SCALA: Which data structures are optimal in which situations when using ".contains()" or ".exists()"?

I would like to know in which situations which data structures are optimal for using "contains" or "exists" checks. I ask because I come from a Python background and am used to using if x in something: expressions for everything. For example, which…
TimY
  • 5,256
  • 5
  • 44
  • 57
20
votes
4 answers

Scala Convert Set to Map

How do I convert a Set("a","b","c") to a Map("a"->1,"b"->2,"c"->3)? I think it should work with toMap.
Torsten Schmidt
  • 207
  • 1
  • 2
  • 6
20
votes
5 answers

Why is filter in front of foldLeft slow in Scala?

I wrote an answer to the first Project Euler question: Add all the natural numbers below one thousand that are multiples of 3 or 5. The first thing that came to me was: (1 until 1000).filter(i => (i % 3 == 0 || i % 5 == 0)).foldLeft(0)(_ + _) but…
andyczerwonka
  • 4,230
  • 5
  • 34
  • 57
20
votes
4 answers

Catching an exception within a map

What is the best way of handling exceptions while iterating over a loop in Scala? For instance, if I had a convert() method that could throw an exception, I'd like to catch that exception, log it, and keep iterating. Is there a "scala" way to do…
shj
  • 201
  • 1
  • 2
  • 3
20
votes
5 answers

What type to use to store an in-memory mutable data table in Scala?

Each time a function is called, if it's result for a given set of argument values is not yet memoized I'd like to put the result into an in-memory table. One column is meant to store a result, others to store arguments values. How do I best…
Ivan
  • 63,011
  • 101
  • 250
  • 382
20
votes
2 answers

Why does this compile under Java 7 but not under Java 8?

This seems to compile fine with Java 7, and any version of the Scala libraries: public static void main(String[] args) { scala.collection.immutable.Set set = new scala.collection.immutable.HashSet(); Iterator iterator…
chiastic-security
  • 20,430
  • 4
  • 39
  • 67
20
votes
3 answers

In Scala 2.8 collections, why was the Traversable type added above Iterable?

I know that to be Traversable, you need only have a foreach method. Iterable requires an iterator method. Both the Scala 2.8 collections SID and the "Fighting Bitrot with Types" paper are basically silent on the subject of why Traversable was added.…
Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
20
votes
3 answers

Scala collection type for filter

Assume you have a List(1,"1") it is typed List[Any], which is of course correct and expected. Now if I map the list like this scala> List(1, "1") map { | case x: Int => x | case y: String => y.toInt | } the resulting type is…
Joa Ebert
  • 6,565
  • 7
  • 33
  • 47
19
votes
2 answers

Getting the maximum key value pair in a Scala map by value

I am trying to pull the maximum value form a map along with its key. For example: val map = Map('a' -> 100, 'b' -> 23, ... 'z' -> 56) Where 100 is the largest value, how would I go about pulling ('a',100)? I essentially want to use Map.max but…
pmaurais
  • 734
  • 2
  • 9
  • 30
19
votes
3 answers

iterating over Scala collections in Java

How I can iterate over Scala collections in Java?
barroco
  • 3,018
  • 6
  • 28
  • 38
19
votes
4 answers

Migrating Java TreeMap code to Scala?

I am migrating my Java code base to pure Scala and I am stuck on this one piece of code. I have an implementation of an IntervalMap i.e. a data structures that let's you efficiently map ranges [from,to] to values where the set, delete and get…
pathikrit
  • 32,469
  • 37
  • 142
  • 221
19
votes
2 answers

Why does Scala's toSeq convert an immutable Set to a mutable ArrayBuffer?

If I call toSeq on an immutable Set collection I get an ArrayBuffer. scala> Set(1,2,3).toSeq // returns Seq[Int] = ArrayBuffer(1, 2, 3) This surprises me. Given Scala's emphasis on using immutable data structures, I expect to get back an immutable…
W.P. McNeill
  • 16,336
  • 12
  • 75
  • 111
19
votes
2 answers

How to implement generic average function in scala?

It seems easy problem for any specific kind of Number i.e. Double/Integer but it is hard to write in general case. implicit def iterebleWithAvg(data:Iterable[Double]) = new { def avg:Double = data.sum / data.size } How to implement this for any…
yura
  • 14,489
  • 21
  • 77
  • 126
18
votes
4 answers

Scala String Equality Question from Programming Interview

Since I liked programming in Scala, for my Google interview, I asked them to give me a Scala / functional programming style question. The Scala functional style question that I got was as follows: You have two strings consisting of alphabetic…
18
votes
4 answers

idiomatic "get or else update" for immutable.Map?

What is the idiomatic way of a getOrElseUpdate for immutable.Map instances?. I use the snippet below, but it seems verbose and inefficient var map = Map[Key, Value]() def foo(key: Key) = { val value = map.getOrElse(key, new Value) map += key ->…
IttayD
  • 28,271
  • 28
  • 124
  • 178