Dave's Brain

Browse - Programming Tips - What's the nice way to set values in a Scala map?

Date: 2013may15
Language: Scala

Q.  What's the nice way to set values in a Scala map?

A.  You can use equals like a regular assignment.
For example...

In Java:

import java.util.HashMap; HashMap<String,String> h = new HashMap<String,String>(); h.put("key", "val"); // Ugly function call syntax
In Scala:
import collection.mutable var h = mutable.HashMap[String,String]() h("key") = "val" // The nice equals syntax

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.