Questions tagged [scala-collections]

Collection library for Scala Programming Language

Related Tags:

1639 questions
47
votes
4 answers

how to remove key value from map in scala

Map(data -> "sumi", rel -> 2, privacy -> 0, status -> 1,name->"govind singh") how to remove data from this map , if privacy is 0. Map(rel -> 2, privacy -> 0, status -> 1,name->"govind singh")
Govind Singh
  • 15,282
  • 14
  • 72
  • 106
46
votes
2 answers

mutable vs. immutable in Scala collections

I am fairly new to Scala and am trying to understand the collections hierarchy. I see that there is a distinction between 'mutable' and 'immutable' collections, but I don't understand what this actually means at the implementation level and how…
astay13
  • 6,857
  • 10
  • 41
  • 56
43
votes
1 answer

Scala convert Iterable or collection.Seq to collection.immutable.Seq

It appears the toSeq method in Scala collections returns a scala.collection.Seq, I could also return a Traversable or Iterable but need to convert this to a scala.collection.immutable.Seq. Is there an easy way to do this? Thanks Richard
Richard Todd
  • 2,406
  • 5
  • 32
  • 40
42
votes
2 answers

remove first and last Element from scala.collection.immutable.Iterable[String]

I am trying to convert my way of getting values from Form, but stuck some where val os= for { m <- request.body.asFormUrlEncoded v <- m._2 } yield v os is scala.collection.immutable.Iterable[String] and when i print it in console os map…
Govind Singh
  • 15,282
  • 14
  • 72
  • 106
41
votes
2 answers

How to define an Ordering in Scala?

Having val hm: HashMap[org.joda.time.DateTime, MyType] I am trying to get the first and the last DateTime of the set by means of hm.keys.min and hm.keys.max respectively but the compiler says No implicit Ordering defined for org.joda.time.DateTime.…
Ivan
  • 63,011
  • 101
  • 250
  • 382
40
votes
4 answers

Why is zipped faster than zip in Scala?

I have written some Scala code to perform an element-wise operation on a collection. Here I defined two methods that perform the same task. One method uses zip and the other uses zipped. def ES (arr :Array[Double], arr1 :Array[Double])…
40
votes
4 answers

Scala foldLeft on Maps

How do you use Map.foldLeft? According to the docs it looks like foldLeft [B] (z: B)(op: (B, (A, B)) ⇒ B) : B But I'm having difficulty: Map("first"->1,"second"->2).foldLeft(0)((a,(k,v)) => a+v ) error: not a legal formal parameter The error…
Pengin
  • 4,692
  • 6
  • 36
  • 62
39
votes
2 answers

scala parallel collections degree of parallelism

Is there any equivalent in scala parallel collections to LINQ's withDegreeOfParallelism which sets the number of threads which will run a query? I want to run an operation in parallel which needs to have a set number of threads running.
Steve Severance
  • 6,611
  • 1
  • 33
  • 44
39
votes
7 answers

How to find duplicates in a list?

I have a list of unsorted integers and I want to find those elements which have duplicates. val dup = List(1,1,1,2,3,4,5,5,6,100,101,101,102) I can find the distinct elements of the set with dup.distinct, so I wrote my answer as follows. val dup =…
Phil
  • 46,436
  • 33
  • 110
  • 175
39
votes
1 answer

Scala for loop over two lists simultaneously

I have a List[Message] and a List[Author] which have the same number of items, and should be ordered so that at each index, the Message is from the Author. I also have class that we'll call here SmartMessage, with a constructor taking 2 arguments: a…
Blackbird
  • 2,368
  • 4
  • 26
  • 41
39
votes
8 answers

Scala: how to merge a collection of Maps

I have a List of Map[String, Double], and I'd like to merge their contents into a single Map[String, Double]. How should I do this in an idiomatic way? I imagine that I should be able to do this with a fold. Something like: val newMap =…
Jeff
  • 14,831
  • 15
  • 49
  • 59
37
votes
3 answers

Difference between mapValues and transform in Map

In Scala Map (see API) what is the difference in semantics and performance between mapValues and transform ? For any given map, for instance val m = Map( "a" -> 2, "b" -> 3 ) both m.mapValues(_ * 5) m.transform( (k,v) => v * 5 ) deliver the same…
elm
  • 20,117
  • 14
  • 67
  • 113
36
votes
5 answers

Difference between MutableList and ListBuffer

What is the difference between Scala's MutableList and ListBuffer classes in scala.collection.mutable? When would you use one vs the other? My use case is having a linear sequence where I can efficiently remove the first element, prepend, and…
Mike
  • 1,839
  • 1
  • 17
  • 25
34
votes
2 answers

Semantics of Scala Traversable, Iterable, Sequence, Stream and View?

There are other questions such as Scala: What is the difference between Traversable and Iterable traits in Scala collections? and How would I get the sum of squares of two Lists in Scala? that answers the question partially. I felt a question that…
smartnut007
  • 6,324
  • 6
  • 45
  • 52
33
votes
1 answer

how to sort a scala.collection.Map[java.lang.String, Int] by its values?

How would you sort a scala.collection.Map[java.lang.String, Int] by its values (so on the Int)? What is a short and elegant way to do that?
Jan Willem Tulp
  • 333
  • 1
  • 3
  • 4