scala hashmap
Scala HashMap (Scala HashMap)
HashMap is a collection based on maps and hashes. It stores key-value pairs.
HashMap是基于地图和哈希的集合。 它存储键值对。
Denoted by:
表示方式:
HashMap<Key, Value> or HashMap<K, V>
Syntax:
句法:
var hashmap = HashMap("key1" -> "value1", ...);
To import HashMap to our Scala program, the following statement is used,
要将HashMap导入我们的Scala程序,请使用以下语句,
scala.collection.mutable.HashMap
Now, let's see the operations on HashMap in Scala,
现在,让我们看看Scala中HashMap上的操作,
Example 1: Creating HashMap in Scala
示例1:在Scala中创建HashMap
Creating a HashMap in Scala is an easy process. A HashMap in Scala can be empty also.
在Scala中创建HashMap是一个简单的过程。 Scala中的HashMap也可以为空。
import scala.collection.mutable.HashMap
object MyClass {
def main(args: Array[String]) {
var hashmap = new HashMap()
var hashmap2 = HashMap(1 -> "K1200" , 2 -> "Thunderbird 350", 3 -> "CBR 1000")
println("Empty HashMap : "+hashmap)
println("Hashmap with elements : "+hashmap2)
}
}
Output
输出量
Empty HashMap : HashMap()
Hashmap with elements : HashMap(1 -> K1200, 2 -> Thunderbird 350, 3 -> CBR 1000)
Example 2: Accessing elements of HashMap
示例2:访问HashMap的元素
The elements of a HashMap can be accessed in Scala using foreach loop as shown below,
可以使用以下所示的foreach循环在Scala中访问HashMap的元素,
import scala.collection.mutable.HashMap
object MyClass {
def main(args: Array[String]) {
var hashmap = HashMap(1 -> "K1200" , 2 -> "Thunderbird 350", 3 -> "CBR 1000")
hashmap.foreach
{
case (key, value) => println (key + " -> " + value)
}
}
}
Output
输出量
1 -> K1200
2 -> Thunderbird 350
3 -> CBR 1000
Example 3: Adding elements to the HashMap
示例3:将元素添加到HashMap
Adding of elements to the HashMap can also be done in Scala programming language. The + operator is used to add new key-value pair to the HashMap.
也可以使用Scala编程语言将元素添加到HashMap中。 +运算符用于将新的键值对添加到HashMap。
import scala.collection.mutable.HashMap
object MyClass {
def main(args: Array[String]) {
var hashmap = HashMap(1 -> "K1200" , 2 -> "Thunderbird 350", 3 -> "CBR 1000")
println("The HashMap is : "+hashmap)
println("Adding new elements to the HashMap. ")
hashmap += (7 -> "HD Fat Boy")
println("The HashMap is : "+hashmap)
}
}
Output
输出量
The HashMap is : HashMap(1 -> K1200, 2 -> Thunderbird 350, 3 -> CBR 1000)
Adding new elements to the HashMap.
The HashMap is : HashMap(1 -> K1200, 2 -> Thunderbird 350, 3 -> CBR 1000, 7 -> HD Fat Boy)
Example 4: Removing element from HashMap in Scala
示例4:从Scala的HashMap中删除元素
In Scala removing elements from a HashMap is also possible. The - operator is used to remove elements from HashMap.
在Scala中,还可以从HashMap中删除元素。 -运算符用于从HashMap中删除元素。
import scala.collection.mutable.HashMap
object MyClass {
def main(args: Array[String]) {
var hashmap = HashMap(1 -> "K1200" , 2 -> "Thunderbird 350", 3 -> "CBR 1000")
println("The HashMap is : "+hashmap)
println("removing elements to the HashMap. ")
hashmap -= 3
println("The HashMap is : "+hashmap)
}
}
Output
输出量
The HashMap is : HashMap(1 -> K1200, 2 -> Thunderbird 350, 3 -> CBR 1000)
removing elements to the HashMap.
The HashMap is : HashMap(1 -> K1200, 2 -> Thunderbird 350)
scala hashmap