我是斯卡拉的新手。我想要为所有下面的数据创建地图,其中包含作为键的PINCODE和作为值的所有其他字段。
<!DOCTYPE html>
<html>
<body>
<table border="1">
<tr>
<th>PINCODE</th>
<th>Locality</th>
<th>PO_TYPE</th>
<th>TALUK</th>
<th>DISTRICT</th>
</tr>
<tr>
<td>500001</td>
<td>Hyderabad G.P.O.</td>
<td>Branch Post Office</td>
<td>Hyderabad</td>
<td>HYDERABAD</td>
</tr>
<tr>
<td>500001</td>
<td>Gandhi Bhawan</td>
<td>Branch Post Office</td>
<td>Nampally</td>
<td>HYDERABAD</td>
</tr>
<tr>
<td>500001</td>
<td>Hindi Bhawan</td>
<td>Branch Post Office</td>
<td>Nampally</td>
<td>HYDERABAD</td>
</tr>
<tr>
<td>500002</td>
<td>Hyderabad Jubilee</td>
<td>Branch Post Office</td>
<td>HYDERABAD</td>
<td>HYDERABAD</td>
</tr>
<tr>
<td>500002</td>
<td>Moghalpura Branch</td>
<td>Post Office</td>
<td>HYDERABAD</td>
<td>HYDERABAD</td>
</tr>
</table>
</body>
</html>例如:(输出类似于下面的内容)
(500001,(海得拉巴邮政分局,海得拉巴,海得拉巴),(甘地巴旺,分局,南帕利,海得拉巴),(印第巴旺,分局,南帕利,海得拉巴)
(500002,(海得拉巴邮政分局,海得拉巴,海得拉巴),(Moghalpura分局,邮局,海得拉巴,海得拉巴)
提前感谢
发布于 2016-12-08 08:58:49
您需要MultiMap,例如:
val mm = new mutable.HashMap[Int, mutable.Set[String]] with mutable.MultiMap[Int, String]
mm.addBinding(500001, "a")
mm.addBinding(500003, "b")
mm.addBinding(500001, "c")
val l = mm.getOrElse(500001, List())
println(l)对于List值类型,可以设置MultiMap值类型为:List[String],如:
val mm = new mutable.HashMap[Int, mutable.Set[List[String]]] with mutable.MultiMap[Int, List[String]]
mm.addBinding(500001, List("a", "b"))
mm.addBinding(500003, List("b", "c"))
mm.addBinding(500001, List("c", "D"))
val l = mm.getOrElse(500001, Set())
println(l)输出:
Set(List(e, f), List(a, b))发布于 2016-12-08 07:51:04
实际上,Map应该是将一个key转换为一个value。
因此,如果您想在同一个键上存储多个值(假设为Strings),则可以将其设置为Map[String, List[String]]。
val map: Map[String, List[String]] = Map(
"1" -> List("val_1_1", "val_1_2", "val_1_3"),
"2" -> List("val_2_1", "val_2_2")
)但是..。至于您的情况,看起来您正在尝试拥有“值”,这些值不是String的,而是更像地址描述的。
在这种情况下,为什么不为地址创建一个类呢?
case class Address(
locality: String,
poType: String,
taluk: String,
district: String
)
// Now you can have you map
val map: Map[String, List[Address]] = Map(
"500001" -> List(Address("Hyderabad G.P.O.", "Branch Post Office", "Hyderabad", "HYDERABAD"))
)
// define a function that we will use to add addresses without over-writing
def updateMapByAddingAddressWithPincode(
map: Map[String, List[String]],
pincode: String,
address: Address
) = {
val existingAddressListForPincode = map.getOrElse(pincode, default = List.empty[Address])
map + (pincode -> address :: existingAddressListForPincode)
}
// now lets say, you want to add another address with same pincode "500001"
val newAddress = Address("Gandhi Bhawan", "Branch Post Office", "Nampally", "HYDERABAD")
val updatedMap = updateMapByAddingAddressWithPincode(
map,
"500001",
newAddress
)发布于 2016-12-08 08:26:22
基本上,您可以将Map[String, Set[String]]与此处描述的特性MultiMap混合在一起:
http://www.scala-lang.org/api/current/scala/collection/mutable/MultiMap.html
示例:
// first import all necessary types from package `collection.mutable`
import collection.mutable.{ HashMap, MultiMap, Set }
// to create a `MultiMap` the easiest way is to mixin it into a normal
// `Map` instance
val mm = new HashMap[Int, Set[String]] with MultiMap[Int, String]
// to add key-value pairs to a multimap it is important to use
// the method `addBinding` because standard methods like `+` will
// overwrite the complete key-value pair instead of adding the
// value to the existing key
mm.addBinding(1, "a")
mm.addBinding(2, "b")
mm.addBinding(1, "c")https://stackoverflow.com/questions/41034077
复制相似问题