0
votes

I am using Scala 2.12.3 and I have a Map[String, Set[String]] and I convert this Map to a Json String by using mapper.writeValueAsString(map). Later I call a web service for which I send this Json String as Request parameter. But the question is, I don't want any Pair my map should have more than 100 elements for each entry. For this example I am using 2 as limit. If there are any, then my plan is to split these exceeding elements for the particular Key and prepare Map again, create the Json String again and call the webserver. I need to repeat this until I call the web service for all the entries in Map.

class ABC(productType: String, productValue: String)    

val mapper: ObjectMapper = new ObjectMapper()   
val keySet = [ABC("abc", "123"), ABC("def", "456"), ABC("abc", "675"), ABC("abc", "982"), ABC("abc", "211"), ABC("def", "321"), ABC("xyz", "908")]
val requestMap = keysSet.foldLeft(Map[String, Set[String]] ()) { (k,v) =>
  val type = v.productType match {
    case "abc" => "abcList"
    case "def" => "defList"
    case "xyz" => "xyzList"
  }
  k+k.get(productType).map(x=> productValue-> (x+v.productType)).getOrElse(productValue-> Set(v.productType))
}

val json = mapper.writeValueAsString(requestMap)

I am expecting , the last line val json = mapper.wrtieValueAsString should be in a loop with each requestMap should have at the max 2 elements for each "abc", "def" and "xyz".

1
What is this? type keyword in identifier position, no vals in ABC, weird [-]-parens for something list-like, bunch of singleton types in all kind of places where it doesn't belong? Are you sure this compiles in 2.12? Also, definitions of mapper and client are not in the example, are they really relevant for the question?Andrey Tyukin
type - I just used this as example, I should have named differently. Let me update. mapper is not relevant, as I just mentioned here to show what I am doing with the map after I get it. client is also not relevant , I just mentioned here to show what I am doing with the json string after I get it.Naveen C
1 - The more code you add that is "not relevant" the harder it is to find and focus on what is relevant. 2 - Your code doesn't compile. After fixing 4 compiler errors I just gave up on trying to understand your question.jwvh
Updated the code by removing "not relevant".Naveen C
It still doesn't compile.jwvh

1 Answers

-1
votes

What you seem to want is to turn your Map[String, Set[String]] into Map[String, List[Set[String]]] first, so that each Set in the List is small enough. Or maybe you would rather go with List[Map[String, Set[String]]] instead.

But I don't see a way around the set-splitting step. You don't have to materialize the List in either case. I would not try to make it a one-liner for readability sake.