How to merge new buffer to old buffer in scala? -
i try merge new data old buffer when refreshing.
update or insert accroding "id" attr.
could tell me how in scala?
def merge(oldbuf: buffer[java.util.map[string, value]], newbuf: buffer[java.util.map[string, value]]) { // loop newbuffer{ // val item = newbuf(n) // val id = item.get("id") // if same id found in oldbuf: // update new [value] old item in oldbuf // else (can not found id in oldbuf) // add new item oldbuf //} return oldbuf }
if want use java's map
, try putall
:
import java.util object buf extends app { val oldbuf = new util.hashmap[int, string]() oldbuf.put(1, "1") oldbuf.put(2, "2") oldbuf.put(3, "3") oldbuf.put(4, "4") val newbuf = new util.hashmap[int, string]() newbuf.put(4, "4 new") newbuf.put(5, "5") oldbuf.putall(newbuf) println(oldbuf) }
if have scala map
:
import scala.collection._ val oldbuf = mutable.map("a" -> 1, "b" -> 2) val newbuf = immutable.map("a" -> 3, "c" -> 4) oldbuf ++= newbuf
so there no difference.
generally speaking suggest converting java collection scala collection, before start processing in scala (if long term goal replace java code). if use java collections in scala in algorithms, might stay there good.
Comments
Post a Comment