Reputation: 502
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)
(Doc.productType.id)
is to access the key(Doc.Group.id)
is to access the value 1st part of the value(container.containerDoc.GroupOrder)
is to access the 2nd part of value which is what I need to be assigned to result.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
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
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