scala-collections

Convert java.util.stream.Stream to Scala Stream

房东的猫 提交于 2019-12-04 15:43:22
I know how I can use the Java libraries, and I can write some loops that do the stuff I need for me, but the question is more, why is there nothing in scala.collection.JavaConverters or scala.collection.JavaConverstions to convert a java.util.stream.Stream to a scala.collection.immutable.Stream ? I would like to do something like this: def streamFiles(path: Path): Stream[Path] = { Files.newDirectoryStream(path).asScala } But instead I have to write something like this: def streamFiles(path: Path): Stream[Path] = { val path_it : java.util.Iterator[Path] = Files.newDirectoryStream(path).iterator

Is there such a thing as bidirectional maps in Scala?

不打扰是莪最后的温柔 提交于 2019-12-04 15:38:45
问题 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 facility in Scala? Actually I need even more: 3 columns to select any in a triplet by another in a triplet (individual values will never be met more than once in the entire map). But a 2-column bidirectional map can help too. 回答1: Guava has a bimap

Scala: how to traverse stream/iterator collecting results into several different collections

∥☆過路亽.° 提交于 2019-12-04 15:10:25
I'm going through log file that is too big to fit into memory and collecting 2 type of expressions, what is better functional alternative to my iterative snippet below? def streamData(file: File, errorPat: Regex, loginPat: Regex): List[(String, String)]={ val lines : Iterator[String] = io.Source.fromFile(file).getLines() val logins: mutable.Map[String, String] = new mutable.HashMap[String, String]() val errors: mutable.ListBuffer[(String, String)] = mutable.ListBuffer.empty for (line <- lines){ line match { case errorPat(date,ip)=> errors.append((ip,date)) case loginPat(date,user,ip,id) =

How to convert from from java.util.Map to a Scala Map

跟風遠走 提交于 2019-12-04 10:17:44
问题 A Java API returns a java.util.Map<java.lang.String,java.lang.Boolean> ;. 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 java.util.Map<java.lang.String,java.lang.Boolean> You can't do Map.empty ++ javaMap , because the ++ method does not know about Java maps. I tried: scalaMap = Map.empty ++ new collection.jcl.MapWrapper[String,Boolean] { override def underlying = javaMap }

Scala return type when extending collection

那年仲夏 提交于 2019-12-04 08:48:57
If I write class Things extends scala.collection.immutable.HashSet[Int] new Things + 5 Then the result is res0: scala.collection.immutable.HashSet[Int] = Set(5) What do I need to do for the result to be of type Things ? I suspect the answer might be here , but am struggling to understand what's going on. I can't really tell without a sample of your code but you can take a look at Jesse Eichar's post which provides a walk through the implementation of a custom collection and addresses your question nicely. http://daily-scala.blogspot.com/2010/04/creating-custom-traversable.html 来源: https:/

Scala Map foreach

蹲街弑〆低调 提交于 2019-12-04 07:52:29
问题 given: val m = Map[String, Int]("a" -> 1, "b" -> 2, "c" -> 3) m.foreach((key: String, value: Int) => println(">>> key=" + key + ", value=" + value)) why does the compiler complain error: type mismatch found : (String, Int) => Unit required: (String, Int) => ? 回答1: I'm not sure about the error, but you can achieve what you want as follows: m.foreach(p => println(">>> key=" + p._1 + ", value=" + p._2)) That is, foreach takes a function that takes a pair and returns Unit , not a function that

Generics invariant covariant contravariant in scala

谁说胖子不能爱 提交于 2019-12-04 07:26:11
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 that Note: subtyping of generic types is invariant . This means that if we have a stack of characters of type Stack[Char] then it cannot be used as an integer stack of type Stack[Int]. This would be unsound because it would enable us to enter true integers into the character stack. To conclude, Stack[A] is only a subtype of Stack[B] if and only if B

Scala Collections inconsistencies

谁说胖子不能爱 提交于 2019-12-04 07:25:45
Why is there a lack of consistency between Sets and Lists in Scala Collections API? For example, there is immutable Set, but also a mutable one. If I want to use the latter, I can simply do this: val set = Set[A]() set += new A However, there is no mutable List, per se. If I want to write a similar code snippet using Lists, which data structure to use? LinkedList sounds as a good candidate, because it is mutable, but has no += method defined. ListBuffer seems to satisfy the requirements, but it is not a list. After reading 2.8 Collections docs I come to the conclusion MutableList is probably

Scala Seq GroupBy with Future

佐手、 提交于 2019-12-04 05:48:02
问题 I have 2 case classes case class First(firstId: Long, pt: Long, vt: Long) case class Second(secondId: Int, vt: Long, a: Long, b: Long, c: Long, d: Long) I have one collection (data:Seq[First]). There is one function which transforms this sequence to another Seq[Second] after applying groupBy and one future operation. getFutureInt is some function returns Future[Int] val output: Future[Seq[Second]] = Future.sequence(data.groupBy(d => (d.vt, getFutureInt(d.firstId))).map {case(k, v) => k._2.map

Extending Scala collections

让人想犯罪 __ 提交于 2019-12-04 04:30:07
问题 I would like to derive a version of a Scala built-in collection that expands on the functionality for a particular generic type e.g., import scala.collection.immutable._ class Tuple2Set[T1,T2] extends HashSet[Tuple2[T1,T2]] { def left = map ( _._1 ) def right = map ( _._2 ) } However when I try to use it with the following test new Tuple2Set[String,String]() + (("x","y")) left I get the following compile error error: value left is not a member of scala.collection.immutable.HashSet[(String,