Danish Ansari
06/15/2021, 1:40 PMMap<String,String>
with case insensitive String
key?
More context and detailed scenario in the thread...Danish Ansari
06/15/2021, 1:40 PMMap<String,String>
like this
val input = mutableMapOf<String, String>().apply {
put("@Username", "John Doe")
// OR
// put("@USERNAME", "John Doe")
// OR
// put("@username", "John Doe")
}
And I have to use username
at lots of different places so I have created a getter extension on Map<String,String>
like this
interval val Map<String, String>.username: String?
get() {
val caseInsensitiveMap = TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER)
caseInsensitiveMap.putAll(this)
return caseInsensitiveMap["@USERNAME"]
}
I know this is not a very optimized way, that's why the question 😬
And I will access it like this
val username = input.username
println(username) // Must print "John Doe"
Arkadii Ivanov
06/15/2021, 1:49 PMDanish Ansari
06/15/2021, 1:50 PMMap
because it's already in use at many place in the project, so I can't change the key nowstreetsofboston
06/15/2021, 1:51 PMstreetsofboston
06/15/2021, 1:52 PMArkadii Ivanov
06/15/2021, 1:58 PMNikky
06/15/2021, 1:59 PMlowercase()
on the key on insert ?
that would not chang the type.. but it would avoid duplicatesDanish Ansari
06/15/2021, 2:01 PMMap
to TreeMap
Vadim Kapustin
06/17/2021, 6:36 AMval Map<String, String>.username: String?
get() = this.asSequence()
.filter { it.key.uppercase() == "@USERNAME" }
.firstOrNull()?.value
@Danish Ansari maybe like this...Vadim Kapustin
06/17/2021, 6:58 AMDanish Ansari
06/17/2021, 6:58 AM