I have the following method where I want to return a map by performing a reduceLeft on a list. My issue is that occasionally the list is empty so Im not sure the correct way to deal with that:
def results(start: String, end: String) = {
val iter = new QueryIterator(RK, start, end);
val list = for (hcol <- iter) yield (Map(hcol.getValue() ->
Map(hcol.getName()) -> hcol.getTime()))))
list.reduceLeft(_ ++ _)
}
When the list is empty it throws an exception that stops the execution. What is the best way to get around this problem?
You can use
foldLeftinstead and start with an empty map of the type that you want to return, e.g.(Make sure you properly match the type of the map; I’m guessing that
getValue()returns anInt, etc..)