dedpo
dedpo

Reputation: 502

Spark - not getting a int from a value of a map in scala

I have a object that is defined as

pLinkGroupsByP: Map[String, Set[(String, Int)]]

I am trying to get Int in the object to be returned if it exists otherwise return 0 thus the .getOrElse(0)

val result:Int =
  Try{
    pLinkGroupsByP(Doc.productType.id)
      .contains(Doc.Group.id,container.containerDoc.GroupOrder)
       (Doc.Group.id,container.containerDoc.GroupOrder)._2
  }.getOrElse(0)

However, I am getting only the first number of the first value in the map no matter what. I just need the number in the value to be assigned back to the result in this try statement.

Sample data is

Map["pro-ucw32, Set[(PRD-1292, 5)]]

Upvotes: 0

Views: 144

Answers (2)

dedpo
dedpo

Reputation: 502

 val tupleToMap = pLinkGroupsByP(Doc.productType.id).toMap
    tupleToMap(relatedDoc.lkGroup.id).GetorElse(0)

The tuple to map converts value is assigned a tuple to map,

relatedDoc.linkGroup.id is a key being mapped and retunred if its a number greater than 1 it will be returned and then a 0

Upvotes: 0

Ramesh Maharjan
Ramesh Maharjan

Reputation: 41987

I guess you can get the result with simple if else

val result:Int = if(pLinkGroupsByP(Doc.productType.id).contains(Doc.Group.id,container.containerDoc.GroupOrder)) container.containerDoc.GroupOrder else 0

Upvotes: 1

Related Questions