Questions tagged [scala-collections]

Collection library for Scala Programming Language

Related Tags:

1639 questions
7
votes
3 answers

Scala: How can I split a String into a Map

Environment : Scala 2.10+ IDE : Eclipse Kepler I have a line NAME=bala AGE=23 COUNTRY=Singapore How can I get it as a map Map(NAME -> bala, AGE -> 23, COUNTRY -> Singapore)
BalaB
  • 3,687
  • 9
  • 36
  • 58
7
votes
2 answers

Casting java.util.LinkedHashMap to scala.collection.mutable.Map

import scala.collection.JavaConversions._ val m = new java.util.LinkedHashMap[String,Int] val s: scala.collection.mutable.Map[String,Int] = m.asInstanceOf[scala.collection.mutable.Map[String,Int]] returns the following…
deepkimo
  • 3,187
  • 3
  • 23
  • 21
7
votes
6 answers

split a stream in many

I'd like to know if there a elegant way to achieve something like that: val l = Stream.from(1) val parts = l.some_function(3) //any number parts.foreach( println(_) ) > 1,4,7,10... > 2,5,8,11... > 3,6,9,12... Actually I need such operation on…
Mikhail Golubtsov
  • 6,285
  • 3
  • 29
  • 36
7
votes
1 answer

Generic type parameters for conversion to parallel collection

I have this function to convert an Array to a ParArray, giving the number of threads as parameter: def parN[T](collection: Array[T], n: Int) = { val parCollection = collection.par parCollection.tasksupport = new ForkJoinTaskSupport( …
ValarDohaeris
  • 6,064
  • 5
  • 31
  • 43
7
votes
1 answer

Scala empty a list

I have a member variable in a class: val options = mutable.LinkedList[SelectOption]() I latter then populate this list from the database. At some point I want to refresh the list. How do I empty it? In java: options.clear(); Is there an equivalent…
Adam Davies
  • 2,742
  • 4
  • 33
  • 52
7
votes
3 answers

Multiple flatMaps in Scala

Instead of xs map f map g it's more efficient to write xs map { x => g(f(x)) }, and similarly for multiple filter operations. If I have two or more flatMaps in a row, is there a way to combine them into one, that's maybe more efficient? e.g. def…
Luigi Plinge
  • 50,650
  • 20
  • 113
  • 180
6
votes
1 answer

Why does BitSet require an explicit cast to be considered as an instance of Set[Int]?

In the scaladoc, BitSet is defined as extending Set[Int]. So I thought using a BitSet as in instance of Set[Int] would work, but I get a type mismatch: Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29). scala>…
Julien Gaugaz
  • 321
  • 3
  • 10
6
votes
3 answers

Writing a generic 'fill' method

I am trying to write a generic fill method, and following is what I have come up with so far: scala> import collection.generic.{GenericTraversableTemplate => GTT} import collection.generic.{GenericTraversableTemplate=>GTT} scala> import…
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
6
votes
5 answers

Why stream fold operation throws Out of memory exception?

I have following simple code def fib(i:Long,j:Long):Stream[Long] = i #:: fib(j, i+j) (0l /: fib(1,1).take(10000000)) (_+_) And it throws OutOfMemmoryError exception. I can not understand why, because I think all the parts use constant memmory…
yura
  • 14,489
  • 21
  • 77
  • 126
6
votes
4 answers

reduce list of integers/range of integers in scala

Total newbie question here...Today while trying to calculate sum of a list of integers(actually BitSet), I ran into overflow scenarios and noticed that the return type of(sum/product) is Int. Are there any methods in Range/List to sum up or say…
Ajay
  • 977
  • 2
  • 11
  • 23
6
votes
3 answers

Merging huge sets (HashSet) in Scala

I have two huge (as in millions of entries) sets (HashSet) that have some (<10%) overlap between them. I need to merge them into one set (I don't care about maintaining the original sets). Currently, I am adding all items of one set to the other…
Alexandros
  • 2,097
  • 20
  • 27
6
votes
2 answers

What are the differences between LazyList and List in Scala?

What are the differences between the following collections types in Scala: List and LazyList types?
Dina Bogdan
  • 4,345
  • 5
  • 27
  • 56
6
votes
1 answer

Use cases of Scala collection forwarders and proxies

Scala's collection library contains the forwarders IterableForwarder, TraversableForwarder, SeqForwarder and proxies like IterableProxy, MapProxy, SeqProxy, SetProxy, TraversableProxy, etc. Forwarders and proxies both delegate collection methods to…
Frank S. Thomas
  • 4,725
  • 2
  • 28
  • 47
6
votes
3 answers

Why collect with pattern match cannot narrow a specific class?

Let's consider following trait: sealed trait AB case class A(a: Int) extends AB case class B(b: Int) extends AB I am trying to collect to limit a collection to specific subclass. If I try to collect, matching individual components and reassembling…
6
votes
3 answers

Generics invariant covariant contravariant in scala

This could be a very silly question, but I am not able to understand the difference even after scratching my head for a long time. I am going through the page of scala generics: https://docs.scala-lang.org/tour/generic-classes.html Here, it is said…
vijayinani
  • 2,548
  • 2
  • 26
  • 48