![]() ![]() Returns a new map with all the key/value mappings of this map except mappings with a key equal to a key from the traversable object xs. Returns a new map containing all the mappings of this map except mappings with a key equal to elem1, elem2 or any of elems. Returns a new map containing mappings of this map and those provided by xs.ĭef -(elem1: A, elem2: A, elems: A*): Map For a complete list of methods available, please check official documentation of Scala. Println("Maroon key exists with value :" + colors("maroon"))įollowing are the important methods which you can use while playing with Maps. ![]() Println("Red key exists with value :" + colors("red")) Try the Following example program to key checking. You can use either Map.contains method to test if a given key exists in the map or not. Here, we used method foreach associated with iterator to walk through the keys. ![]() You can iterate through the keys and values of a Map using “foreach” loop. Println( "colors1 ++ colors2 : " + colors ) use two or more Maps with ++ as operator Try the following example program to concatenate two Maps. You can use either ++ operator or Map.++() method to concatenate two or more Maps, but while adding Maps it will remove duplicate keys. Values in colors : MapLike(#FF0000, #F0FFFF, #CD853F) The following commands are used to compile and execute this program. Println( "Check if nums is empty : " + nums.isEmpty ) Println( "Check if colors is empty : " + colors.isEmpty ) Println( "Values in colors : " + colors.values ) Println( "Keys in colors : " + colors.keys ) Try the following example program showing usage of the Map methods. This method returns true if the map is empty otherwise false. This method returns an iterable containing each value in the map. This method returns an iterable containing each key in the map. If we want to add a key-value pair to a Map, we can use the operator + as follows.Īll operations on maps can be expressed in terms of the following three methods. While defining empty map, the type annotation is necessary as the system needs to assign a concrete type to variable. Empty hash table whose keys are strings and values are integers: The Following is the example statements to declare immutable Maps − If you want to use both mutable and immutable Maps in the same, then you can continue to refer to the immutable Map as Map but you can refer to the mutable set as mutable.Map. If you want to use the mutable Map, you'll have to import class explicitly. The difference between mutable and immutable objects is that when an object is immutable, the object itself can't be changed.īy default, Scala uses the immutable Map. There are two kinds of Maps, the immutable and the mutable. Keys are unique in the Map, but values need not be unique. Any value can be retrieved based on its key. Scala map is a collection of key/value pairs.
0 Comments
Leave a Reply. |