fork download
  1. import collection.mutable.ListBuffer
  2.  
  3. object MapUtils {
  4. // var data : Map[String,ListBuffer[(String,Long)]] = Map()
  5. val data = collection.mutable.Map[String, ListBuffer[(String, Long)]]()
  6.  
  7. def addData(key : String, message : String) : Unit = {
  8. val newTuple = (message, System.currentTimeMillis())
  9. val optionalOldValue = data.get(key)
  10.  
  11. optionalOldValue match {
  12. case Some(olderBufferList) => olderBufferList += newTuple
  13. case None => data
  14. .put(key, ListBuffer[(String, Long)](newTuple))
  15. }
  16. }
  17. }
  18.  
  19. object Main extends App {
  20. MapUtils.addData("123", "message 1")
  21. MapUtils.addData("456", "message 2")
  22.  
  23. println(MapUtils.data)
  24. // Map(456 -> ListBuffer((message 2,1472925061065)), 123 -> ListBuffer((message 1,1472925060926)))
  25. }
Success #stdin #stdout 0.17s 322432KB
stdin
Standard input is empty
stdout
Map(456 -> ListBuffer((message 2,1472925612375)), 123 -> ListBuffer((message 1,1472925612333)))