我对Scala编程完全不熟悉,我有以下问题:
我需要一个HashMap,它可以包含许多数据类型(Int,String等).在C++中,我会使用BOOST的MultiMap.我听说在Scala中有一个MultiMap特征可用.基本上我想要的是以下内容:
val map = HashMap[String, ListBuffer[_]]
Run Code Online (Sandbox Code Playgroud)
ListBuffer元素的具体数据类型将在运行时确定.当我在控制台中测试此实现时,以下工作:
scala> val a = new HashMap[String, ListBuffer[_]]()
a: scala.collection.mutable.HashMap[String,scala.collection.mutable.ListBuffer[_]] = Map()
scala> val b = new ListBuffer[String]()
b: scala.collection.mutable.ListBuffer[String] = ListBuffer()
scala> val c = new ListBuffer[Int]()
c: scala.collection.mutable.ListBuffer[Int] = ListBuffer()
scala> b += "String"
res0: b.type = ListBuffer(String)
scala> c += 1
res1: c.type = ListBuffer(1)
scala> a += "String Buffer" -> b
res2: a.type = Map((String Buffer,ListBuffer(String)))
scala> a += "This is an Int Buffer" -> c
res3: a.type = …Run Code Online (Sandbox Code Playgroud) 我在listbuffer类型上使用prepend方法并观察一些奇怪的行为.prepend操作返回一个可接受的新列表.但是它不应该修改ListBuffer吗?在预先添加之后,我仍然看到ListBuffer的长度没有改变.我在这里错过了什么吗?
scala> val buf = new ListBuffer[Int]
buf: scala.collection.mutable.ListBuffer[Int] = ListBuffer()
scala> buf += 1
res47: buf.type = ListBuffer(1)
scala> buf += 2
res48: buf.type = ListBuffer(1, 2)
scala> 3 +: buf
res49: scala.collection.mutable.ListBuffer[Int] = ListBuffer(3, 1, 2)
scala> buf.toList
res50: List[Int] = List(1, 2)
Run Code Online (Sandbox Code Playgroud) 在这段注释中的代码中,正确显示了列表缓冲区项的长度,但是在第二注释中,代码从未执行。为什么会发生?
val conf = new SparkConf().setAppName("app").setMaster("local")
val sc = new SparkContext(conf)
var wktReader: WKTReader = new WKTReader();
val dataSet = sc.textFile("dataSet.txt")
val items = new ListBuffer[String]()
dataSet.foreach { e =>
items += e
println("len = " + items.length) //1. here length is ok
}
println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
items.foreach { x => print(x)} //2. this code doesn't be executed
Run Code Online (Sandbox Code Playgroud)
日志在这里:
16/11/20 01:16:52 INFO Utils: Successfully started service 'SparkUI' on port 4040.
16/11/20 01:16:52 INFO SparkUI: Bound SparkUI to 0.0.0.0, and started at http://192.168.56.1:4040 …Run Code Online (Sandbox Code Playgroud) 我在ListBuffer上创建了循环:
val listOfNamesOfMissingFiles = new ListBuffer[String]()
controlFileContent.entries.foreach { entry =>
val fileName = entry("File_Name")
val sha256 = entry("File_SHA_256")
val file = new File(folder, fileName)
if (!file.exists()) {
logger.error(s"Control file validation failed: file does not exist in folder: [$fileName].")
listOfNamesOfMissingFiles += fileName
}
Run Code Online (Sandbox Code Playgroud)
我想将我在listOfNamesOfMissingFiles中获得的内容分配给一个字符串到attribut,我将其作为事件发送到kibana门户网站.如果我的listOfNamesOfMissingFiles == ListBuffer("hhhh","uuuu").如果我要做listOfNamesOfMissingFiles.toString我会得到"ListBuffer("hhhh","uuuu")".如何从输出中删除ListBuffer?