scala-collections

Maximum Length for scala queue

依然范特西╮ 提交于 2019-12-17 23:55:13
问题 I'm curious if Scala has some gem hidden in its collection classes that I can use. Basically I'm looking for something like a FIFO queue, but that has an upper-limit on its size such that when the limit is hit, the oldest (first) element is removed from the queue. I've implemented this myself in Java in the past, but I'd rather use something standard if possible. 回答1: An often preferable alternative to subclassing is the (unfortunately named) "pimp my library" pattern. You can use it to add

How to get the element index when mapping an array in Scala?

泄露秘密 提交于 2019-12-17 22:38:20
问题 Let's consider a simple mapping example: val a = Array("One", "Two", "Three") val b = a.map(s => myFn(s)) What I need is to use not myFn(s: String): String here, but myFn(s: String, n: Int): String , where n would be the index of s in a . In this particular case myFn would expect the second argument to be 0 for s == "One", 1 for s == "Two" and 2 for s == "Three". How can I achieve this? 回答1: Depends whether you want convenience or speed. Slow: a.zipWithIndex.map{ case (s,i) => myFn(s,i) }

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

妖精的绣舞 提交于 2019-12-17 16:36:08
问题 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 take the array a , we would have a,x , a,y , b,x , b,y , c,x , c,y . I know of zip but from what I've seen it only works on collections of the same sizes, and it associates elements from same positions. 回答1: I'm not sure of a "method", but this can

Difference between Array and List in scala

谁都会走 提交于 2019-12-17 14:59:13
问题 In what cases I should use Array(Buffer) and List(Buffer). Only one difference that I know is that arrays are nonvariant and lists are covariant. But what about performance and some other characteristics? 回答1: Immutable Structures The Scala List is an immutable recursive data structure which is such a fundamental structure in Scala, that you should (probably) be using it much more than an Array (which is actually mutable - the immutable analog of Array is IndexedSeq ). If you are coming from

Scala best way of turning a Collection into a Map-by-key?

寵の児 提交于 2019-12-17 10:08:59
问题 If I have a collection c of type T and there is a property p on T (of type P , say), what is the best way to do a map-by-extracting-key ? val c: Collection[T] val m: Map[P, T] One way is the following: m = new HashMap[P, T] c foreach { t => m add (t.getP, t) } But now I need a mutable map. Is there a better way of doing this so that it's in 1 line and I end up with an immutable Map? (Obviously I could turn the above into a simple library utility, as I would in Java, but I suspect that in

Difference between MutableList and ListBuffer

流过昼夜 提交于 2019-12-17 09:20:09
问题 What is the difference between Scala's MutableList and ListBuffer classes in scala.collection.mutable ? When would you use one vs the other? My use case is having a linear sequence where I can efficiently remove the first element, prepend, and append. What's the best structure for this? 回答1: A little explanation on how they work. ListBuffer uses internally Nil and :: to build an immutable List and allows constant-time removal of the first and last elements. To do so, it keeps a pointer on the

Convert java.util.HashMap to scala.collection.immutable.Map in java

只谈情不闲聊 提交于 2019-12-17 06:38:23
问题 I'm using some Scala library from my Java code. And I have a problem with collections. I need to pass scala.collection.immutable.Map as a parameter of a method. I can convert or build immutable.Map from my Java code but I do not know how to do it. Suggestions? 回答1: It's entirely possible to use JavaConverters in Java code—there are just a couple of additional hoops to jump through: import java.util.HashMap; import scala.Predef; import scala.Tuple2; import scala.collection.JavaConverters;

Scala Map implementation keeping entries in insertion order?

◇◆丶佛笑我妖孽 提交于 2019-12-17 06:14:32
问题 In Java, I use LinkedHashMap for this purpose. The documentation of Java's LinkedHashMap is very clear that it has "predictable iteration order" and I need the same in Scala. Scala has ListMap and LinkedHashMap , but the documentation on what they do exactly is poor. Question: Is Scala's LinkedHashMap or ListMap the implementation to use for this purpose? If not, what other options are available besides using the Java's LinkedHashMap directly? 回答1: From the LinkedHashMap Scaladoc page: "This

What type to use to store an in-memory mutable data table in Scala?

坚强是说给别人听的谎言 提交于 2019-12-17 06:08:59
问题 Each time a function is called, if it's result for a given set of argument values is not yet memoized I'd like to put the result into an in-memory table. One column is meant to store a result, others to store arguments values. How do I best implement this? Arguments are of diverse types, including some enums. In C# I'd generally use DataTable. Is there an equivalent in Scala? 回答1: You could use a mutable.Map[TupleN[A1, A2, ..., AN], R] , or if memory is a concern, a WeakHashMap [1]. The

What type to use to store an in-memory mutable data table in Scala?

假如想象 提交于 2019-12-17 06:06:03
问题 Each time a function is called, if it's result for a given set of argument values is not yet memoized I'd like to put the result into an in-memory table. One column is meant to store a result, others to store arguments values. How do I best implement this? Arguments are of diverse types, including some enums. In C# I'd generally use DataTable. Is there an equivalent in Scala? 回答1: You could use a mutable.Map[TupleN[A1, A2, ..., AN], R] , or if memory is a concern, a WeakHashMap [1]. The