Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single key contains Multiple values in Map in scala

Tags:

html

scala

I am New to scala. I want to create map for all bellow data contains PINCODE as key and All other field as value.

<!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>

for example: (output is something like bellow)

(500001, (Hyderabad G.P.O.,Branch Post Office,Hyderabad,HYDERABAD), (Gandhi Bhawan,Branch Post Office,Nampally,HYDERABAD), (Hindi Bhawan,Branch Post Office,Nampally,HYDERABAD))

(500002 ,(Hyderabad Jubilee,Branch Post Office,HYDERABAD,HYDERABAD), (Moghalpura Branch,Post Office,HYDERABAD,HYDERABAD))

Thanks in advance

like image 223
Darshan Manek Avatar asked Oct 17 '25 10:10

Darshan Manek


2 Answers

You are wanting MultiMap, example:

  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)

For List value type, you can set the MultiMap value type is: List[String], like:

  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)

Output:

  Set(List(e, f), List(a, b))
like image 179
chengpohi Avatar answered Oct 20 '25 01:10

chengpohi


Actually a Map is supposed to uniquely map a key to a value.

So, if you want to store multiple value (lets assume Strings) on the same key, you can make it a 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")
)

But... as for your case it looks like you are trying have "values" which are not Strings but look more like address descriptions.

In this case why not create a class for Address ?

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
)
like image 40
sarveshseri Avatar answered Oct 20 '25 00:10

sarveshseri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!