Questions tagged [scala-collections]

Collection library for Scala Programming Language

Related Tags:

1639 questions
11
votes
1 answer

What does the single-right-arrow (→) mean for collections in Scala?

When I use auto-complete on lists on InteliJ, it shows a single-right-arrow, with no documentation on what it means. It looks like →. In an example, it's called as: val sampleList: List[String] = List("a", "b", "c"); sampleList.→() I don't know…
sparkonhdfs
  • 1,313
  • 2
  • 17
  • 31
11
votes
2 answers

Scala Iterator with Map and For

Given: val list = List("one","two","three") val it = list.toIterator I can run: list map ("_" +) -> List(_one, _two, _three) for (i <- list) yield("_" + i) -> List(_one, _two, _three) If I run the same on the iterator I get: it map ("_" + )…
ssanj
  • 2,169
  • 3
  • 19
  • 28
11
votes
2 answers

How does map() on 'zipped' Lists work?

I am looking to calculate the scalar product of two lists. Let's say we have two Lists, l1 = List(1,2,3) and l2 = List(4,5,6), the result should be List(4,10,18) The code below works: def scalarProduct(l1 : List[Int], l2 : List[Int]):List[Int] =…
Ankit Khettry
  • 997
  • 1
  • 13
  • 33
11
votes
1 answer

Scala - why Double consume less memory than Floats in this case?

Here's a strange behavior I fell into and I can't find any hint on why it's like this. I use in this example the estimate method of SizeEstimator from Spark but I haven't found any glitch in their code so I wonder why - if they provide a good…
Vince.Bdn
  • 1,145
  • 1
  • 13
  • 28
11
votes
5 answers

Merge Sets of Sets that contain common elements in Scala

I want to implement a function in Scala, that, given a Set of Sets of Ints will merge any containing Set that contains one or more common elements. So for example, given: def mergeSets(sets: Set[Set[Int]]): Set[Set[Int]] = ??? val sets =…
spyk
  • 878
  • 1
  • 9
  • 26
11
votes
3 answers

Are the head and tail of a Set guaranteed to be mutually exclusive?

The documentation says that Set.head returns the "first" item, and .tail returns "all but the first".* Since a Set doesn't really have a "first" item, the documentation warns that without an ordered type, you might get a different result on…
Ben Kovitz
  • 4,920
  • 1
  • 22
  • 50
11
votes
2 answers

Scala mutable collections and "Reference must be prefixed warnings"

I have to use a mutable linked list for a specific use case. However I'd like to avoid "Reference must be prefixed" warnings. Aliasing the import seems to be a solution: import scala.collection.mutable.{LinkedList => MutableLinkedList} it works…
ArtisanV
  • 511
  • 4
  • 14
11
votes
4 answers

Unmodifiable view of a mutable Scala collection

I have a class with a private field that is a mutable collection. The field in this particular instance is an ArrayBuffer, although my question extends to any finite, ordered, random-access collection type. I want to expose this field without…
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
11
votes
3 answers

How to use Java Collections.shuffle() on a Scala array?

I have an array that I want to permutate randomly. In Java, there is a method Collections.shuffle() that can shuffle the elements of a List randomly. It can be used on an array too: String[] array = new String[]{"a", "b", "c"}; // Shuffle the…
Jesper
  • 202,709
  • 46
  • 318
  • 350
10
votes
2 answers

Scala: Contains in mutable and immutable sets

I've discovered a strange behavior for mutable sets which I cannot understand: I have a object which I want to add to a set. The equals method for the class is overridden. When I add two different objects to the set, which produces the same output…
Stefan
  • 111
  • 5
10
votes
2 answers

Is there a Scala version of NavigableMap?

In Java 1.6, the NavigableMap (and the NavigableSet) interfaces were introduced and TreeMap was updated to implement the new interface. Among other things, NavigableMap is useful for asking questions like "Which element in the collection is closest…
Jim Hurne
  • 7,187
  • 4
  • 44
  • 44
10
votes
2 answers

Scala Map: mysterious syntactic sugar?

I have just found out this syntax for a scala Map (used here in mutable form) val m = scala.collection.mutable.Map[String, Int]() m("Hello") = 5 println(m) //PRINTS Map(Hello -> 5) Now I'm not sure whether this is syntactic sugar built in to the…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
10
votes
3 answers

Scala equivalent of new HashSet(Collection)

What is the equivalent Scala constructor (to create an immutable HashSet) to the Java new HashSet(c) where c is of type Collection?. All I can find in the HashSet Object is apply.
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
10
votes
3 answers

Dealing with the surprising lack of ParList in scala.collections.parallel

So scala 2.9 recently turned up in Debian testing, bringing the newfangled parallel collections with it. Suppose I have some code equivalent to def expensiveFunction(x:Int):Int = {...} def process(s:List[Int]):List[Int} =…
timday
  • 24,582
  • 12
  • 83
  • 135
10
votes
5 answers

Scala method to combine each element of an iterable with each element of another?

If I have this: val a = Array("a ","b ","c ") val b = Array("x","y") I would like to know if such a method exists which would let me traverse the first collection, and for each of it's elements, walk the entire second collection. For example, if we…
Geo
  • 93,257
  • 117
  • 344
  • 520