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.
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.
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.
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.
S -> Scala J -> Java C -> Csharp After Removing S -> Scala J -> Java
- Scala Tutorial – Learn Scala with Step By Step Guide
- Scala short <(x: Short): Boolean
- Scala short <(x: Char): Boolean
- Scala Extractors
- Scala | Partially Applied functions
- Scala String indexOf(String str) method with example
- Scala String contentEquals() method with example
- Scala Keywords
- Scala Int /(x: Int) method with example
- Scala Int /(x: Short) method with example
- Program to print Java Set of characters in Scala
- Scala Map size() method with example
- Scala SortedMap addString() method with a start, a separator and an end with example
- Scala Iterator addString() method with example
- Scala String substring(int beginIndex, int endIndex) method with example
- Scala | Functions Call-by-Name
- Program to convert Java list to an iterator in Scala
- Scala Set &() method with example
- Scala | Type Inference
- Program to convert Java set of Shorts to an Indexed Sequence in Scala
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.