Questions tagged [scala-collections]

Collection library for Scala Programming Language

Related Tags:

1639 questions
15
votes
4 answers

Compare two Maps in Scala

Is there any pre-defined function that I can use to compare two Maps based on the key and give me the difference? Right now, I iterate Map1 and foreach key, I check if there is an element in Map2 and I pattern match to find the difference. Is there…
joesan
  • 13,963
  • 27
  • 95
  • 232
15
votes
4 answers

scala.collection.breakOut vs views

This SO answer describes how scala.collection.breakOut can be used to prevent creating wasteful intermediate collections. For example, here we create an intermediate Seq[(String,String)]: val m = List("A", "B", "C").map(x => x -> x).toMap By using…
theon
  • 14,170
  • 5
  • 51
  • 74
14
votes
4 answers

Can't append to scala's mutable LinkedList?

I'm looking at the API and the :+ method returns a new LinkedList. The append method will only allow the appending of another linked list. The += method needs a var to work. Why would anyone ever need these if the LinkedList is mutable? What…
dpington
  • 1,844
  • 3
  • 17
  • 29
14
votes
1 answer

Using collect on maps in Scala

I recently stumbled over this post, which "introduces" the collect method for Scala collections. The usage is straight forward: scala> val ints = List(1, "2", 3) collect { case i: Int => i } ints: List[Int] = List(1, 3) Now maps are basically lists…
agilesteel
  • 16,775
  • 6
  • 44
  • 55
14
votes
2 answers

Scala collection memory footprint characteristics

There is a handy page about performance characteristics of the Scala collection classes. Is there similar data on memory footprint? I have a situation where I'm concerned about memory use and would like to factor this in my choice of collection to…
huynhjl
  • 41,520
  • 14
  • 105
  • 158
14
votes
3 answers

Can I use scala List directly in Java?

Can I use scala List in Java, like : import scala.collection.immutable.List; class HelloScalaList { public static void main (String[] args) { List xs = List(1, 2, 3); System.out.println(xs); } } It does not seem to compile.…
Visus Zhao
  • 1,144
  • 2
  • 12
  • 25
14
votes
5 answers

What's a good and functional way to swap collection elements in Scala?

In a project of mine one common use case keeps coming up. At some point I've got a sorted collection of some kind (List, Seq, etc... doesn't matter) and one element of this collection. What I want to do is to swap the given element with it's…
Andreas Eisele
  • 743
  • 1
  • 9
  • 19
14
votes
6 answers

List of every n-th item in a given list

This is a simple exercise I am solving in Scala: given a list l return a new list, which contains every n-th element of l. If n > l.size return an empty list. def skip(l: List[Int], n: Int) = Range(1, l.size/n + 1).map(i => l.take(i *…
Michael
  • 41,026
  • 70
  • 193
  • 341
14
votes
3 answers

In Scala, how to check if a Map contains all entries from another Map?

Total newb question. Say I have 2 maps val map1 = Map("ram"->"2gb", "size"->"15", "color"->"red", "fruit"->"strawberry") val map2 = Map("ram"->"2gb", "size"->"15", "color"->"red") and I want to know if map1 fully contains map2 (extra key/values in…
Thomas
  • 181
  • 2
  • 5
14
votes
6 answers

O(1) conversion from mutable.Map to immutable.Map?

Is there a way to convert (wrap) a mutable Map to immutable in O(1) time (that is, not by copying the values, but similar to what is done in JavaConversions)
IttayD
  • 28,271
  • 28
  • 124
  • 178
14
votes
7 answers

Combining multiple Lists of arbitrary length

I am looking for an approach to join multiple Lists in the following manner: ListA a b c ListB 1 2 3 4 ListC + # * § % .. .. .. Resulting List: a 1 + b 2 # c 3 * 4 § % In Words: The elements in sequential order, starting at first list combined…
Th 00 mÄ s
  • 3,776
  • 1
  • 27
  • 46
14
votes
4 answers

Scala Buffer: Size or Length?

I am using a mutable Buffer and need to find out how many elements it has. Both size and length methods are defined, inherited from separate traits. Is there any actual performance difference, or can they be considered exact synonyms?
Eduardo
  • 8,362
  • 6
  • 38
  • 72
14
votes
7 answers

Iterate through odd members of collection in Scala

What is an efficient way to iterate through only the odd members of a collection in Scala, based on index position? Given this list: val fruits: List[String] = List("apples", "oranges", "pears", "bananas") I want to to skip apples and pears, and…
kgx
  • 1,195
  • 3
  • 15
  • 26
14
votes
4 answers

How to convert from from java.util.Map to a Scala Map

A Java API returns a java.util.Map;. I would like to put that into a Map[String,Boolean] So imagine we have: var scalaMap : Map[String,Boolean] = Map.empty val javaMap = new JavaClass().map() // Returns…
George
  • 3,433
  • 4
  • 27
  • 25
13
votes
3 answers

How to convert a mutable HashMap into an immutable equivalent in Scala?

Inside a function of mine I construct a result set by filling a new mutable HashMap with data (if there is a better way - I'd appreciate comments). Then I'd like to return the result set as an immutable HashMap. How to derive an immutable from a…
Ivan
  • 63,011
  • 101
  • 250
  • 382