Tuesday, 7 May 2019

Map in Scala (Extended)


HashMap
HashMap is known as HashMap because it uses a technique called Hashing. Hashing is a technique of converting a large String to small String that represents same String. A shorter value helps in indexing and faster searches.
Some points on HashMap:
HashMap doesn’t allow duplicate keys but allows duplicate values. That means A single key can’t contain more than 1 value but more than 1 key can contain a single value.
HashMap allows null key also but only once and multiple null values.
This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time. It is roughly similar to HashTable but is unsynchronized.
Internal Structure of HashMap
Internally HashMap contains an array of Node and a node is represented as a class which contains 4 fields :
int hash, K key, V value & Node next
HashMap is preferrably used where you want the key values to be unique while storing information.
Initialize a HashMap with 3 elements
import scala.collection.mutable.HashMap
println("\nInitialize a HashMap with 3 elements")
var hashMap1: HashMap[String, String] = HashMap(("PD","Plain"),("SD","Strawberry"),("CD","Chocolate"))
//Output
Initialize a HashMap with 3 elements
hashMap1: scala.collection.mutable.HashMap[String,String] = Map(CD -> Chocolate, SD -> Strawberry, PD -> Plain)
initialize HashMap using key -> value notation
The code below shows how to initialize HashMap using key -> value notation.
import scala.collection.mutable.HashMap
println("\nInitialize HashMap using key -> value notation")
val hashMap2: HashMap[String, String] = HashMap("VD"-> "Vanilla", "GD" -> "Glazed")
//Output
Initialize HashMap using key -> value notation
hashMap2: scala.collection.mutable.HashMap[String,String] = Map(GD -> Glazed, VD -> Vanilla)
Access elements of HashMap by specific key
val hashMap2: HashMap[String, String] = HashMap("VD"-> "Vanilla", "GD" -> "Glazed")
hashMap2.get("VD").get
//Output:  Vanilla
Add elements to HashMap using +=
import scala.collection.mutable.HashMap
var hashMap1: HashMap[String, String] = HashMap(("PD","Plain"),("SD","Strawberry"))
hashMap1 += ("CD"->"Customer")
hashMap1: scala.collection.mutable.HashMap[String,String] = Map(CD -> Customer, SD -> Strawberry, PD -> Plain)
Add elements from a HashMap to an existing HashMap using ++=
hashMap1 ++= hashMap2
println(hashMap1)
Remove key and its value from HashMap using -=
hashMap1 -= "CD"
//Output
hashMap1: HashMap[String, String] = HashMap(("PD","Plain"),("SD","Strawberry"))
Initialize an empty HashMap
import scala.collection.mutable.HashMap
val emptyMap: HashMap[String,String] = HashMap[String,String]()
Note : Examples having duplicate value for single key using HashMap
import collection.mutable.{ HashMap, MultiMap, Set }
val nHashMap = new HashMap[Int, Set[String]] with MultiMap[Int, String]
nHashMap.addBinding(1,"a")
nHashMap.addBinding(1,"b")
nHashMap.addBinding(1,"c")
nHashMap.addBinding(2,"b")
for((k,v)<-nHashMap)
{
     println("Key value : "+k)
     println("Duplicate entry of key mapped with string : "+v)
 }
//Output
Key value : 2
Duplicate entry of key mapped with string : Set(b)
Key value : 1
Duplicate entry of key mapped with string : Set(c, a, b)

LinkedHashMap
LinkedHashMap is known as LinkedHashMap because it uses a technique called Hashing. Hashing is a technique of converting a large String to small String that represents same String. A shorter value helps in indexing and faster searches.
Some points on LinkedHashMap:
LinkedHashMap doesn’t allow duplicate keys but allows duplicate values. That means A single key can’t contain more than 1 value but more than 1 key can contain a single value.
LinkedHashMap allows null key also but only once and multiple null values.
This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time. It is roughly similar to HashTable but is unsynchronized.
Internal Structure of LinkedHashMap
Internally LinkedHashMap contains an array of Node and a node is represented as a class which contains 4 fields :
int hash, K key, V value & Node next
LinkedHashMap is preferrably used where you want the key values to be unique while storing information.
Initialize a LinkedHashMap with 3 elements
import scala.collection.mutable.LinkedHashMap
println("\nInitialize a LinkedHashMap with 3 elements")
var hashMap1: LinkedHashMap[String, String] = LinkedHashMap(("PD","Plain"),("SD","Strawberry"),("CD","Chocolate"))
//Output
Initialize a LinkedHashMap with 3 elements
hashMap1: scala.collection.mutable.LinkedHashMap[String,String] = Map(CD -> Chocolate, SD -> Strawberry, PD -> Plain)
initialize LinkedHashMap using key -> value notation

The code below shows how to initialize LinkedHashMap using key -> value notation.
import scala.collection.mutable.LinkedHashMap
println("\nInitialize LinkedHashMap using key -> value notation")
val hashMap2: LinkedHashMap[String, String] = LinkedHashMap("VD"-> "Vanilla", "GD" -> "Glazed")
//Output
Initialize LinkedHashMap using key -> value notation
hashMap2: scala.collection.mutable.LinkedHashMap[String,String] = Map(GD -> Glazed, VD -> Vanilla)
Access elements of LinkedHashMap by specific key
val hashMap2: LinkedHashMap[String, String] = LinkedHashMap("VD"-> "Vanilla", "GD" -> "Glazed")
hashMap2.get("VD").get
//Output:  Vanilla
Add elements to LinkedHashMap using +=
import scala.collection.mutable.LinkedHashMap
var hashMap1: LinkedHashMap[String, String] = LinkedHashMap(("PD","Plain"),("SD","Strawberry"))
hashMap1 += ("CD"->"Customer")
hashMap1: scala.collection.mutable.LinkedHashMap[String,String] = Map(CD -> Customer, SD -> Strawberry, PD -> Plain)
Add elements from a LinkedHashMap to an existing LinkedHashMap using ++=
hashMap1 ++= hashMap2
println(hashMap1)
Remove key and its value from LinkedHashMap using -=
hashMap1 -= "CD"
//Output
hashMap1: LinkedHashMap[String, String] = LinkedHashMap(("PD","Plain"),("SD","Strawberry"))
Initialize an empty LinkedHashMap
import scala.collection.mutable.LinkedHashMap
val emptyMap: LinkedHashMap[String,String] = LinkedHashMap[String,String]()

SortedMap


SortedMap uses a technique called Hashing. Hashing is a technique of converting a large Int to small Int that represents same String. A shorter value helps in indexing and faster searches.
import scala.collection.SortedMap
val grades = SortedMap(10->"aakash",4->"kumar",9->"Bihar",1->"piro")
grades
//Output
grades: scala.collection.SortedMap[Int,String] = Map(1 -> piro, 4 -> kumar, 9 -> Bihar, 10 -> aakash)
-Difference between HashMap & LinkedHashMap?

HashMap : - Result or output of hashmap are not in specified order.
LinkedHashMap : Result or output of Linkedhashmap are in specified order.


No comments:

Post a Comment

Use filter method to filter a Scala collection

Use filter method to filter a Scala collection To use filter on your collection, give it a predicate to filter the collection elements as...