I am using M3.1 with the latest IDEA 12 EAP.
I’m trying to convert a regular HashMap to a sorted map.
When I try:
val map = HashMap<String, String>()
map.toSortedMap(String.CASE_INSENSITIVE_ORDER)
then String has the red wiggly underline and it calls String a classifier (which I don't understand).
If I try fully qualifying it:
val map = HashMap<String, String>()
map.toSortedMap(java.lang.String.CASE_INSENSITIVE_ORDER)
then toSortedMap gets the red wiggly underline & it tells me that type inference has failed (Cannot infer type parameter K).
What is the right way to access String’s static field?
Thanks,
Neil
B7W
2
You make 50% of work :-)
In kotlin String != java String. So it is right to call java.lang.String.CASE_INSENSITIVE_ORDER
Kotlin think that all things came from java are Nullable.
val CASE_INSENSITIVE_ORDER : Comparator<String?>
But we can fix it with external annotations. Go to definition of CASE_INSENSITIVE_ORDER, and do this http://blog.jetbrains.com/kotlin/using-external-annotations/ In our case we need to set it:
val CASE_INSENSITIVE_ORDER : Comparator<String>
Working sample
val map = hashMap<String, String>(“2” to “2”, “1” to “1”, “3” to “3”)
println(map) //{3=3, 2=2, 1=1}
val sorted = map.toSortedMap(java.lang.String.CASE_INSENSITIVE_ORDER)
println(sorted) //{1=1, 2=2, 3=3}
1 Like