i have following types:
case class category(id: int, name: string) case class product(id: int, location: location, categoryidlist: option[list[int]]) given list of products
val products:list[product] = loadproducts() how can product map of categoryid location?
map[categoryid, location] so method this:
def getmap(products: list[product]): map[int, location] = { ?? } i need somehow iterate on optional list of categoryidlist , create map location property.
in order convert seq map need first convert seq[(int,location)], seq of tuple2. tomap method available.
edit: okay here's implementation based on each categoryid on list, note shouldn't use option of list, since empty state list empty list.
def getmap(products: list[product]): map[int, location] = { products.flatmap(tolisttuple2).tomap } def tolisttuple2(product: product): list[(int, location)] = { product.categoryidlist .getorelse(list()) .map(category => (category, product.location)) } so here first turn our product list of categoryids , locations , flatmap list of (int, location), can turned map calling tomap.
Comments
Post a Comment