Open In App

HashMap in Scala

Last Updated : 09 Aug, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

HashMap is a part of Scala Collection’s. It is used to store element and return a map. A HashMap is a combination of key and value pairs which are stored using a Hash Table data structure. It provides the basic implementation of Map.

Syntax:

var hashMapName = HashMap("key1"->"value1", "key2"->"value2", "key3"->"value3", ...) 

We must import scala.collection.mutable.HashMap for HashMap.

Operation performed by HashMap

Creating an HashMap:

Below is the example to create HashMap. In below code, we can see an empty HashMap is created then a HashMap is created with values.




// Scala program to create or print HashMap
import scala.collection.mutable.HashMap
  
// Creating object 
object Geeks 
  
    // Main method 
    def main(args: Array[String]) 
    
        // Creating empty HashMap
        var hashMap = new HashMap()  
          
        // Creating HashMap with values
        var hashMap2 = HashMap("C"->"Csharp", "S"->"Scala", "J"->"Java")  
          
        // Printing HashMap
        println(hashMap)  
        println(hashMap2)  
    


Output:

Map()
Map(S -> Scala, J -> Java, C -> Csharp)

 
Adding and Accessing Elements :

In the below example, A HashMap is created. add elements and access elements also performed.




// Scala program to Adding and Accessing Elements HashMap
import scala.collection.mutable.HashMap
  
// Creating object 
object Geeks 
  
    // Main method 
    def main(args: Array[String]) 
    
        // Creating HashMap
        var hashMap = HashMap("C"->"Csharp", "S"->"Scala", "J"->"Java")
          
        // Iterating elements
        hashMap.foreach 
        {  
            case (key, value) => println (key + " -> " + value)         
        }  
          
        // Accessing value by using key 
        println(hashMap("S"))  
          
        // Adding element
        var HashMap2 = hashMap + ("P"->"Perl")  
        HashMap2.foreach 
        {  
            case (key, value) => println (key + " -> " + value)  
        }  
    


Output:

S -> Scala
J -> Java
C -> Csharp
Scala
S -> Scala
P -> Perl
J -> Java
C -> Csharp

 
Removing an element from HashMap :
A HashMap is created than removing an element is performed using sign. Below is the example to removing an element from HashMap.




// Scala program to removing Element HashMap
import scala.collection.mutable.HashMap
  
// Creating object 
object Geeks 
  
    // Main method 
    def main(args: Array[String]) 
    
        // Creating HashMap
        var hashMap = HashMap("C"->"Csharp", "S"->"Scala", "J"->"Java")
          
        // Iterating elements
        hashMap.foreach 
        
            case (key, value) => println (key + " -> " + value)         
        
          
        // Removing an element
        hashMap -= "C"
          
        println("After Removing")
        hashMap.foreach 
        
            case (key, value) => println (key + " -> " + value) 
        
    


Output:

S -> Scala
J -> Java
C -> Csharp
After Removing
S -> Scala
J -> Java


Previous Article
Next Article

Similar Reads

Scala short <(x: Short): Boolean
Short, a 16-bit signed integer (equivalent to Java's short primitive type) is a subtype of scala.AnyVal. The <(x: Short) method is utilized to return true if this value is less than x, false otherwise. Method Definition: def <(x: Short): Boolean Return Type: It returns true if this value is less than x, otherwise false. Example #1: // Scala p
1 min read
Scala short <(x: Char): Boolean
Short, a 16-bit signed integer (equivalent to Java's short primitive type) is a subtype of scala.AnyVal. The <(x: Char) method is utilized to return true if this value is less than x, false otherwise. Method Definition: def <(x: Char): BooleanReturn Type: It returns true if this value is less than x, otherwise false. Example #1: C/C++ Code //
1 min read
Scala Extractors
In Scala Extractor is defined as an object which has a method named unapply as one of its part. This method extracts an object and returns back the attributes. This method is also used in Pattern matching and Partial functions. Extractors also explains apply method, which takes the arguments and constructs an object so, it's helpful in constructing
6 min read
Scala | Partially Applied functions
The Partially applied functions are the functions which are not applied on all the arguments defined by the stated function i.e, while invoking a function, we can supply some of the arguments and the left arguments are supplied when required. we call a function we can pass less arguments in it and when we pass less arguments it does not throw an ex
3 min read
Scala String indexOf(String str) method with example
The indexOf(String str) method is utilized to return the index of the sub-string which occurs first in the string stated. Method Definition: indexOf(String str) Return Type: It returns the index of the sub-string which is specified in the argument of the method. Example #1: // Scala program of int indexOf() // method // Creating object object GfG {
1 min read
Scala String contentEquals() method with example
The contentEquals() method is utilized to compare a string to the content of StringBuffer. Method Definition: Boolean contentEquals(StringBuffer sb) Return Type: It returns true if the content is equal to the stated string else it returns false. Example #1: // Scala program of contentEquals() // method // Creating object object GfG { // Main method
1 min read
Scala Keywords
Keywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to use as variable names or objects. Doing this will result in a compile-time error. Example: // Scala Program to illustrate the keywords // Here object, def, and var are valid ke
2 min read
Scala Int /(x: Int) method with example
The /(x: Int) method is utilized to return the quotient when the specified first int value is divided by the second int value. Method Definition: (First_Int_Value)./(Second_Int_Value) Return Type: It returns the quotient when the specified first int value is divided by the second int value. Example #1: // Scala program of Int /(x: Int) // method //
1 min read
Scala Int /(x: Short) method with example
The /(x: Short) method is utilized to return the quotient when the specified int value is divided by the short value. Method Definition: (Int_Value)./(Short_Value) Return Type: It returns the quotient when the specified int value is divided by the short value. Example #1: // Scala program of Int /(x: Short) // method // Creating object object GfG {
1 min read
Program to print Java Set of characters in Scala
A java Set of characters can be returned from a Scala program by writing a user defined method of Java in Scala. Here, we don't even need to import any Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples. Example:1# // Scala program to print Java Set // of characters in Scala // Creating object object
2 min read
Article Tags :
Practice Tags :