Questions tagged [scala-collections]

Collection library for Scala Programming Language

Related Tags:

1639 questions
33
votes
4 answers

Which scala mutable list to use?

This is a followup question to No Scala mutable list I want to use a mutable list in Scala. I can chose…
Karel Bílek
  • 36,467
  • 31
  • 94
  • 149
33
votes
4 answers

Cleaner tuple groupBy

I have a sequence of key-value pairs (String, Int), and I want to group them by key into a sequence of values (i.e. Seq[(String, Int)]) => Map[String, Iterable[Int]])). Obviously, toMap isn't useful here, and groupBy maintains the values as tuples.…
Tomer Gabel
  • 4,104
  • 1
  • 33
  • 37
32
votes
1 answer

Conversion from scala parallel collection to regular collection

I'm trying to convert back from a parallel collection to a regular map. According to the api, if I call toMap on any appropriately defined parallel collection, it's supposed to return a standard Map, but it's returning ParMap over a flattened…
JasonMond
  • 1,440
  • 1
  • 16
  • 30
31
votes
4 answers

Google guava vs Scala collection framework comparison

There are a lot of common concepts: immutable collection, collection view, strict/non strict collection, collection builders same patterns in Guava and Scala Collection API. So what is the difference? Is both library are consistent with…
yura
  • 14,489
  • 21
  • 77
  • 126
30
votes
4 answers

How to get the element index when mapping an array in Scala?

Let's consider a simple mapping example: val a = Array("One", "Two", "Three") val b = a.map(s => myFn(s)) What I need is to use not myFn(s: String): String here, but myFn(s: String, n: Int): String, where n would be the index of s in a. In…
Ivan
  • 63,011
  • 101
  • 250
  • 382
30
votes
4 answers

Scala collection standard practice

Coming from a Java background, I'm used to the common practice of dealing with collections: obviously there would be exceptions but usually code would look like: public class MyClass { private Set mySet; public void init() { …
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
30
votes
1 answer

Idiomatic Scala Map upsert

I'm working with a map in Scala and doing the usual "if there's no value associated with a key, create it, put it in the map and return it": def alphaMemory(key : AlphaMemoryKey) = { var am = map.getOrElse(key, null) if(am == null) { …
Dave Ray
  • 39,616
  • 7
  • 83
  • 82
30
votes
2 answers

How to find the number of (key , value) pairs in a map in scala?

I need to find the number of (key , value) pairs in a Map in my Scala code. I can iterate through the map and get an answer but I wanted to know if there is any direct function for this purpose or not.
Tanvi
  • 413
  • 1
  • 5
  • 14
29
votes
2 answers

How do I convert an Array[String] to a Set[String]?

I have an array of strings. What's the best way to turn it into an immutable set of strings? I presume this is a single method call, but I can't find it in the scala docs. I'm using scala 2.8.1.
dave4420
  • 46,404
  • 6
  • 118
  • 152
29
votes
3 answers

Scala Nil equivalent for Set

Is there an equivalent of Nil for Set in scala? I tried using Nil as a value for Set, but I got an error (expected since the type of Nil is List) Thanks
Rand
  • 991
  • 3
  • 11
  • 20
28
votes
1 answer

How are Scala 2.9 parallel collections working behind the scenes?

Scala 2.9 introduced parallel collections. They are a really great tool for certain tasks. However, how do they work internally and am I able to influence the behavior/configuration? What method do they use to figure out the optimal number of…
Steffen
  • 8,033
  • 1
  • 28
  • 34
28
votes
3 answers

convert java.util.Map[String, Object] to scala.collection.immutable.Map[String, Any]

How do I convert java.util.Map[String, Object] to scala.collection.immutable.Map[String, Any], so that all values in the original map (integers, booleans etc.) are converted to the right value to work well in Scala.
IttayD
  • 28,271
  • 28
  • 124
  • 178
28
votes
3 answers

Get first n elements from List

I have a List val family=List("1","2","11","12","21","22","31","33","41","44","51","55") i want to take its first n elements but the problem is that parents size is not fixed. val familliar=List("1","2","11") //n=3
john smith
  • 301
  • 1
  • 3
  • 7
28
votes
5 answers

Convert Scala Set into Java (java.util.Set)?

I have a Set in Scala (I can choose any implementation as I am creating the Set. The Java library I am using is expecting a java.util.Set[String]. Is the following the correct way to do this in Scala (using…
27
votes
7 answers

Convert java.util.HashMap to scala.collection.immutable.Map in java

I'm using some Scala library from my Java code. And I have a problem with collections. I need to pass scala.collection.immutable.Map as a parameter of a method. I can convert or build immutable.Map from my Java code but I do not know how to do it.…
user1590420
  • 291
  • 1
  • 3
  • 4