Questions tagged [scala-collections]

Collection library for Scala Programming Language

Related Tags:

1639 questions
26
votes
4 answers

Integrating Scala into an existing project in Java

we have a project written in Java. It is a maven project, that has jsp pages and a lot of java code: servlets for handling user requests and dealing out responses. classes of logic used by servlets in order to process the required logic. classes of…
SPIRiT_1984
  • 2,717
  • 3
  • 29
  • 46
25
votes
2 answers

Create an immutable list from a java.lang.Iterator

I'm using a library (JXPath) to query a graph of beans in order to extract matching elements. However, JXPath returns groups of matching elements as an instance of java.lang.Iterator and I'd rather like to convert it into an immutable scala list.…
Romain Rouvoy
  • 295
  • 1
  • 3
  • 9
25
votes
3 answers

Scala: Why does Seq.contains take an Any argument, instead of an argument of the sequence type?

So for example why does List(1,2,3,4).contains("wtf") even compile? Wouldn't it be nice if the compiler rejected this?
Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
25
votes
6 answers

What is the fastest way to sum a collection in Scala

I've tried different collections in Scala to sum it's elements and they are much slower than Java sums it's arrays (with for cycle). Is there a way for Scala to be as fast as Java arrays? I've heard that in scala 2.8 arrays will be same as in java,…
Tala
  • 8,888
  • 5
  • 34
  • 38
25
votes
5 answers

Type based collection partitioning in Scala

Given the following data model: sealed trait Fruit case class Apple(id: Int, sweetness: Int) extends Fruit case class Pear(id: Int, color: String) extends Fruit I've been looking to implement a segregate basket function which for the given…
Norbert Radyk
  • 2,608
  • 20
  • 24
25
votes
4 answers

Is there a Round Robin/Circular Queue available in Scala Collections

Is there a Round Robin Queue available in Scala Collections? I need to repeatedly iterate a list that circles through itself val x = new CircularList(1,2,3,4) x.next (returns 1) x.next (returns 2) x.next (returns 3) x.next (returns 4) x.next…
user2780187
  • 677
  • 7
  • 16
25
votes
2 answers

Why (copy) appending to Seq in Scala is defined as :+ and not just + as in Set and Map?

Scala's Map and Set define a + operator that returns a copy of the data structure with a single element appended to it. The equivalent operator for Seq is denoted :+. Is there any reason for this inconsistency?
thesamet
  • 6,382
  • 2
  • 31
  • 42
24
votes
6 answers

Is there such a thing as bidirectional maps in Scala?

I'd like to link 2 columns of unique identifiers and be able to get a first column value by a second column value as well as a second column value by a first column value. Something like Map(1 <-> "one", 2 <-> "two", 3 <-> "three") Is there such a…
Ivan
  • 63,011
  • 101
  • 250
  • 382
24
votes
8 answers

How to get a random element from a Set in Scala

For any given set, for instance, val fruits = Set("apple", "grape", "pear", "banana") how to get a random element from fruits ? Many Thanks.
elm
  • 20,117
  • 14
  • 67
  • 113
24
votes
3 answers

How does Scala's mutable Map update [map(key) = newValue] syntax work?

I'm working through Cay Horstmann's Scala for the Impatient book where I came across this way of updating a mutable map. scala> val scores = scala.collection.mutable.Map("Alice" -> 10, "Bob" -> 3, "Cindy" -> 8) scores:…
Dan Midwood
  • 18,694
  • 7
  • 33
  • 32
23
votes
5 answers

java.util.Iterator to Scala list?

I have the following code: private lazy val keys: List[String] = obj.getKeys().asScala.toList obj.getKeys returns a java.util.Iterator Calling asScala, via JavaConverers (which is imported) according to the docs..…
rshepherd
  • 1,396
  • 3
  • 12
  • 21
23
votes
3 answers

Scala Map from tuple iterable

Constructing scala.collection.Map from other collections, I constantly find myself writing: val map = Map(foo.map(x=>(x, f(x))) However, this doesn't really work since Map.apply takes variable arguments only - so I have to write: val map =…
themel
  • 8,825
  • 2
  • 32
  • 31
23
votes
6 answers

How can I find the index of the maximum value in a List in Scala?

For a Scala List[Int] I can call the method max to find the maximum element value. How can I find the index of the maximum element? This is what I am doing now: val max = list.max val index = list.indexOf(max)
Phil
  • 46,436
  • 33
  • 110
  • 175
22
votes
2 answers

Collection type generated by for with yield

When I evaluate a for in Scala, I get an immutable IndexedSeq (a collection with array-like performance characteristics, such as efficient random access): scala> val s = for (i <- 0 to 9) yield math.random + i s:…
Jesper
  • 202,709
  • 46
  • 318
  • 350
21
votes
7 answers

Why don't Scala Lists have an Ordering?

Is there a reason why there is no implicit Ordering for Lists in Scala? val lists = List(List(2, 3, 1), List(2, 1, 3)) lists.sorted error: could not find implicit value for parameter ord: Ordering[List[Int]] EDIT Yes, my question is why there is…
Craig P. Motlin
  • 26,452
  • 17
  • 99
  • 126