Scala Collections Tutorial
Selected Reading
- Scala Collections - Discussion
- Scala Collections - Useful Resources
- Scala Collections - Quick Guide
- Scala Collections - zip
- Scala Collections - scan
- Scala Collections - reduce
- Scala Collections - partition
- Scala Collections - map
- Scala Collections - foldRight
- Scala Collections - foldLeft
- Scala Collections - fold
- Scala Collections - flatten
- Scala Collections - flatMap
- Scala Collections - find
- Scala Collections - filter
- Scala Collections - dropWhile
- Scala Collections - drop
- Scala Collections - Stream
- Scala Collections - Stack
- Scala Collections - Seq
- Scala Collections - Tuple
- Scala Collections - Queue
- Scala Collections - Option
- Scala Collections - Iterator
- Scala Collections - ListMap
- Scala Collections - HashMap
- Scala Collections - Map
- Scala Collections - TreeSet
- Scala Collections - HashSet
- Scala Collections - BitSet
- Scala Collections - Set
- Scala Collections - Vector
- Scala Collections - ListSet
- Scala Collections - ListBuffer
- Scala Collections - List
- Scala Collections - ArrayBuffer
- Scala Collections - Array using Range
- Scala Collections - Multi-Dimensional Array
- Scala Collections - Array
- Scala Collections - Environment Setup
- Scala Collections - Overview
- Scala Collections - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Scala Collections - HashMap
Scala Collections - HashMap
Scala map is a collection of key/value pairs. Any value can be retrieved based on its key. Keys are unique in the Map, but values need not be unique. HashMap implements immutable map and uses hash table to implement the same.
Declaring HashMap Variables
The following is the syntax for declaring an HashMap variable.
Syntax
val colors = HashMap("red" -> "#FF0000", "azure" -> "#F0FFFF", "peru" -> "#CD853F")
Here, colors is declared as an hash-map of Strings, Int which has three key-value pairs. Values can be added by using commands pke the following −
Command
var myMap1: HashMap[Char, Int] = colors + ("black" -> "#000000");
Processing HashMap
Below is an example program of showing how to create, initiapze and process HashMap −
Example
import scala.collection.immutable.HashMap object Demo { def main(args: Array[String]) = { var myMap: HashMap[String,String] = HashMap( "red" -> "#FF0000", "azure" -> "#F0FFFF", "peru" -> "#CD853F" ); // Add an element var myMap1: HashMap[String,String] = myMap + ("white" -> "#FFFFFF"); // Print key values myMap.keys.foreach{ i => print( "Key = " + i ) println(" Value = " + myMap(i) ) } if( myMap.contains( "red" )) { println("Red key exists with value :" + myMap("red")) } else { println("Red key does not exist") } if( myMap.contains( "maroon" )) { println("Maroon key exists with value :" + myMap("maroon")) } else { println("Maroon key does not exist") } //removing element var myMap2: HashMap[String,String] = myMap - ("white"); // Create empty map var myMap3: HashMap[String,String] = HashMap.empty[String, String]; println(myMap1); println(myMap2); println(myMap3); } }
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
>scalac Demo.scala >scala Demo
Output
Key = azure Value = #F0FFFF Key = peru Value = #CD853F Key = red Value = #FF0000 Red key exists with value :#FF0000 Maroon key does not exist HashMap(azure -> #F0FFFF, peru -> #CD853F, white -> #FFFFFF, red -> #FF0000) HashMap(azure -> #F0FFFF, peru -> #CD853F, red -> #FF0000) HashMap()Advertisements