반응형
MultiValueMap to Map
@Test
fun multiValueMapToMap() {
val multiValueMap = LinkedMultiValueMap<String, String>()
multiValueMap["a"] = "a"
multiValueMap["b"] = "b"
multiValueMap["c"] = "c"
val map = multiValueMap.entries.associate { it.key to it.value[0] }
println(map) // {a=a, b=b, c=c}
}
Map to MultiValueMap
@Test
fun mapToMultiValueMap() {
val map = HashMap<String, String>()
map["a"] = "a"
map["b"] = "b"
map["c"] = "c"
val multiValueMap = LinkedMultiValueMap<String, String>().apply { setAll(map) }
println(multiValueMap) // {a=[a], b=[b], c=[c]}
}
Object to MultiValueMap
class Student(
val id: Long,
val name: String
)
@Test
fun objectToMultiValueMap() {
val student = Student(1, "effortguy")
val multiValueMap = jacksonObjectMapper()
.convertValue<Map<String, String>>(student)
.let { LinkedMultiValueMap<String, String>().apply { setAll(it) } }
println(multiValueMap) // {id=[1], name=[effortguy]}
}
반응형
댓글