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…
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…
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…
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…
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…
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.…
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…
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 *…
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…
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)
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…
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?
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…
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…
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…