2

I want to create Kotlin utility accessible from Java that converts list of Strings to Map. So far I've written:

class Utils {
    companion object {
        @JvmStatic fun values(item: GSAItem): Map<String, Object> {
            return item.itemDescriptor.propertyNames.map {it -> Map.Entry<String, Any!>(it, item.getPropertyValue(it)) };            }
    }
}

But I'm getting error

Error:(16, 74) Kotlin: Unresolved reference: Entry

GSAItem.getPropertyValue is Java method which takes String as argument and returns Object. After that I suspect I need to find some equivalent of collect function from Java 8?

4
  • What is the KeyStore class? Commented Aug 30, 2016 at 13:35
  • @yole I'm guessing it's this KeyStore. KeyStore.Entry is a marker interface. Did you mean to use PrivateKeyEntry, SecretKeyEntry or TrustedCertificateEntry instead? Commented Aug 30, 2016 at 13:47
  • Oops, sorry. I've tried different imports and eventually pasted wrong code. I've edited it to match what was original version. Commented Aug 30, 2016 at 13:49
  • 1
    Please don't use empty classes with companion objects. Use plain objects or make the function top-level and consider making it an extension function. Commented Aug 31, 2016 at 10:59

2 Answers 2

1

Map.Entry - stdlib - Kotlin Programming Language is an interface and as such it does not have a constructor which is why you are getting an error (perhaps not the best message). You can find an implementation, make your own, or use associate instead:

class Utils {
    companion object {
        @JvmStatic fun values(item: GSAItem): Map<String, Any?> {
            return item.itemDescriptor.propertyNames.associate { it to item.getPropertyValue(it) }
        }
    }
}

Note that you should use Any or Any? instead of java.lang.Object.

Sign up to request clarification or add additional context in comments.

Comments

1

How about something like this:

item.itemDescriptor
    .propertyNames
    .map { name -> name to item.getPropertyValue(name) }
    .toMap()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.