I am declaring a map literal in my code. This eventually will be a long map and has a chance of keys being repeated that I want to avoid. Also, I don’t want to sort declaration based on keys to keep them grouped based on what source they were picked from and easy to add to the list.
I am using IntelliJ Idea and I’m ok if I can set some inspection preference to highlight any duplicate keys. I tried looking up but didn’t find any. I also tried using detekt plugin to highlight the issue for me, but couldn’t get it working either.
Following is a small example of a map with duplicate keys that Kotlin allows, but I want to be highlighted in the editor so that I can avoid it:
val MF_RR_HeaderMap = mapOf<String, MFRRHeader>(
// Source 1
"unit" to MFRRHeader.UNIT_NUMBER,
"unit type" to MFRRHeader.UNIT_TYPE,
"resident code" to MFRRHeader.TENANT_CODE,
"resident name" to MFRRHeader.TENANT_NAME,
// first entry
"market rent" to MFRRHeader.MARKET_RENT,
// Source 2
"type" to MFRRHeader.UNIT_TYPE,
"sq. feet" to MFRRHeader.SQFT,
"residents" to MFRRHeader.TENANT_NAME,
"status" to MFRRHeader.STATUS,
// This is the duplicate entry that I want to be highlighted
"market rent" to MFRRHeader.STATUS,
)
val MF_RR_HeaderMap = buildMap<String, MFRRHeader> {
infix fun String.to(value: MFRRHeader) =
require(put(this, value) == null) { "Duplicate key $this" }
// Source 1
"unit" to MFRRHeader.UNIT_NUMBER
"unit type" to MFRRHeader.UNIT_TYPE
"resident code" to MFRRHeader.TENANT_CODE
"resident name" to MFRRHeader.TENANT_NAME
// first entry
"market rent" to MFRRHeader.MARKET_RENT
// Source 2
"type" to MFRRHeader.UNIT_TYPE
"sq. feet" to MFRRHeader.SQFT
"residents" to MFRRHeader.TENANT_NAME
"status" to MFRRHeader.STATUS
// This is the duplicate entry that I want to be highlighted
"market rent" to MFRRHeader.STATUS
}
It is indeed cooler to spit out the duplicate keys but duplicates are presumably rare (possibly a programming error), maybe occurring less than 1% of the time, and therefore the duplicate check shouldn’t use much runtime (e.g. by creating a Grouping).