-
-
Save ov7a/3f9b282f1f3b47e8a98299e20df2ea2f to your computer and use it in GitHub Desktop.
MapValue examples for article
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.atomic.AtomicInteger | |
import scala.util.Random | |
object Main { | |
def main(args: Array[String]): Unit = { | |
println("Sample1") | |
sample1() | |
println("Sample2") | |
sample2() | |
println("Sample3") | |
sample3() | |
println("Sample4") | |
sample4() | |
println("Sample5") | |
sample5() | |
} | |
def sample1(): Unit = { | |
val someMap = Map("key1" -> "value1", "key2" -> "value2") | |
def foo(value: String): String = value + "_changed" | |
val resultMap = someMap.map { case (k, v) => k -> foo(v) } | |
val resultMapValues = someMap.mapValues(foo) | |
println(s"resultMap: $resultMap") | |
println(s"resultMapValues: $resultMapValues") | |
println(s"equality: ${resultMap == resultMapValues}") | |
} | |
def sample2(): Unit = { | |
case class ValueHolder(value: String) | |
val someMap: Map[String, String] = Map("key1" -> "value1", "key2" -> "value2") | |
def foo(value: String): ValueHolder = ValueHolder(value) | |
val resultMap: Map[String, ValueHolder] = someMap.map { case (k, v) => k -> foo(v) } | |
val resultMapValues: Map[String, ValueHolder] = someMap.mapValues(foo) | |
println(s"resultMap: $resultMap") | |
println(s"resultMapValues: $resultMapValues") | |
println(s"equality: ${resultMap == resultMapValues}") | |
} | |
def sample3(): Unit = { | |
case class ValueHolder(value: String, seed: Int) | |
val someMap = Map("key1" -> "value1", "key2" -> "value2") | |
def foo(value: String): ValueHolder = ValueHolder(value, Random.nextInt()) | |
val resultMap: Map[String, ValueHolder] = someMap.map { case (k, v) => k -> foo(v) } | |
val resultMapValues: Map[String, ValueHolder] = someMap.mapValues(foo) | |
println(s"resultMap: $resultMap") | |
println(s"resultMapValues: $resultMapValues") | |
println(s"equality: ${resultMap == resultMapValues}") | |
println(s"simple assert for resultMap: ${resultMap.head == resultMap.head}") | |
println(s"simple assert for resultMapValues: ${resultMapValues.head == resultMapValues.head}") | |
println(s"resultMapValues: $resultMapValues") | |
println(s"resultMapValues: $resultMapValues") | |
} | |
def sample4(): Unit = { | |
case class ValueHolder(value: String, seed: Int) | |
val someMap = Map("key1" -> "value1", "key2" -> "value2") | |
def foo(value: String): ValueHolder = ValueHolder(value, Random.nextInt()) | |
val resultMapValues: Map[String, ValueHolder] = someMap.mapValues(foo).map(identity) | |
println(s"resultMapValues: $resultMapValues") | |
println(s"simple assert for resultMapValues: ${resultMapValues.head == resultMapValues.head}") | |
println(s"resultMapValues: $resultMapValues") | |
println(s"resultMapValues: $resultMapValues") | |
} | |
def sample5(): Unit = { | |
val someMap1 = Map("key1" -> new AtomicInteger(0), "key2" -> new AtomicInteger(0)) | |
val someMap2 = Map("key1" -> new AtomicInteger(0), "key2" -> new AtomicInteger(0)) | |
def increment(value: AtomicInteger): Int = value.incrementAndGet() | |
val resultMap: Map[String, Int] = someMap1.map { case (k, v) => k -> increment(v) } | |
val resultMapValues: Map[String, Int] = someMap2.mapValues(increment) | |
println(s"resultMap (1): $resultMap") | |
println(s"resultMapValues (1): $resultMapValues") | |
println(s"resultMap (2): $resultMap") | |
println(s"resultMapValues (2): $resultMapValues") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment