Questions tagged [scala-collections]

Collection library for Scala Programming Language

Related Tags:

1639 questions
84
votes
6 answers

withFilter instead of filter

Is it always more performant to use withFilter instead of filter, when afterwards applying functions like map, flatmap etc.? Why are only map, flatmap and foreach supported? (Expected functions like forall/exists as well)
Kigyo
  • 5,668
  • 1
  • 20
  • 24
83
votes
4 answers

Converting mutable to immutable map

private[this]object MMMap extends HashMap[A, Set[B]] with MultiMap[A, B] How convert it to immutable?
Jeriho
  • 7,129
  • 9
  • 41
  • 57
77
votes
2 answers

How to transform Scala collection of Option[X] to collection of X

I'm starting to explore Scala, and one of the things I'm intrigued by is the Option type and the promise of being able to eliminate null related errors. However I haven't been able to work out how to transform a list (or other collection) of, say,…
npad
  • 5,036
  • 4
  • 24
  • 22
73
votes
1 answer

Scala 2.8 collections design tutorial

Following on from my breathless confusion, what are some good resources which explain how the new Scala 2.8 collections library has been structured. I'm interested to find some information on how the following fit together: The collection…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
66
votes
10 answers

Min/max with Option[T] for possibly empty Seq?

I'm doing a bit of Scala gymnastics where I have Seq[T] in which I try to find the "smallest" element. This is what I do right now: val leastOrNone = seq.reduceOption { (best, current) => if (current.something < best.something) current else…
gustafc
  • 28,465
  • 7
  • 73
  • 99
62
votes
1 answer

Scala Sets contain the same elements, but sameElements() returns false

Whilst working through the Scala exercises on Iterables, I encountered the following strange behaviour: val xs = Set(5,4,3,2,1) val ys = Set(1,2,3,4,5) xs sameElements ys // true val xs = Set(3,2,1) val ys = Set(1,2,3) xs sameElements ys …
DNA
  • 42,007
  • 12
  • 107
  • 146
60
votes
1 answer

How to append or prepend on a Scala mutable.Seq

There's something I don't understand about Scala's collection.mutable.Seq. It describes the interface for all mutable sequences, yet I don't see methods to append or prepend elements without creating a new sequence. Am I missing something obvious…
Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234
60
votes
5 answers

scala Iterable#map vs. Iterable#flatMap

What is the difference between the map and flatMap functions of Iterable?
Landon Kuhn
  • 76,451
  • 45
  • 104
  • 130
55
votes
4 answers

What are views for collections and when would you want to use them?

In Scala, for many (all?) types of collections you can create views. What exactly is a view and for which purposes are views useful?
Jesper
  • 202,709
  • 46
  • 318
  • 350
54
votes
7 answers

Scala Map foreach

given: val m = Map[String, Int]("a" -> 1, "b" -> 2, "c" -> 3) m.foreach((key: String, value: Int) => println(">>> key=" + key + ", value=" + value)) why does the compiler complain error: type mismatch found : (String, Int) => Unit required:…
Dzhu
  • 4,311
  • 5
  • 36
  • 48
51
votes
4 answers

Use-cases for Streams in Scala

In Scala there is a Stream class that is very much like an iterator. The topic Difference between Iterator and Stream in Scala? offers some insights into the similarities and differences between the two. Seeing how to use a stream is pretty simple…
Jesse Eichar
  • 1,081
  • 1
  • 11
  • 11
50
votes
5 answers

How to implement Map with default operation in Scala

class DefaultListMap[A, B <: List[B]] extends HashMap[A, B] { override def default(key: A) = List[B]() } I wan't to create map A -> List[B]. In my case it is Long -> List[String] but when I get key from map that doesn't have value I would…
Lukasz
  • 3,135
  • 4
  • 20
  • 24
50
votes
4 answers

Convert Java Map to Scala Map

I have a java map: java.util.Map> and I would like to convert it to the scala map: Map[SomeObject, Set[OtherObject]] I have used mapAsScalaMap but the result is not quite what I want, the result is:…
user365268
49
votes
4 answers

How to convert a Seq[A] to a Map[Int, A] using a value of A as the key in the map?

I have a Seq containing objects of a class that looks like this: class A (val key: Int, ...) Now I want to convert this Seq to a Map, using the key value of each object as the key, and the object itself as the value. So: val seq: Seq[A] = ... val…
Jesper
  • 202,709
  • 46
  • 318
  • 350
48
votes
6 answers

Scala Map implementation keeping entries in insertion order?

In Java, I use LinkedHashMap for this purpose. The documentation of Java's LinkedHashMap is very clear that it has "predictable iteration order" and I need the same in Scala. Scala has ListMap and LinkedHashMap, but the documentation on what they do…
ebruchez
  • 7,760
  • 6
  • 29
  • 41