7

I had java code which looked like this:

    public static <KEY, VALUE> Map<KEY, List<VALUE>> createMultimap(@NonNull Collection<VALUE> values, @NonNull TransformFunction<VALUE, KEY> transformFunction) {
    Map<KEY, List<VALUE>> tmpMap = new HashMap<>();
    for (VALUE value : values) {
        if (value != null) {
            KEY key = transformFunction.transform(value);
            List<VALUE> valuesList = tmpMap.get(key);
            if (valuesList == null) {
                valuesList = new ArrayList<>();
                tmpMap.put(key, valuesList);
            }
            valuesList.add(value);
        }
    }
    return Collections.unmodifiableMap(tmpMap);
}

I converted it using Android Studio option 'convert to Kotlin'

Here what is look like:

    fun <KEY, VALUE> createMultimap(values: Collection<VALUE>, transformFunction: TransformFunction<VALUE, KEY>): Map<KEY, List<VALUE>> {
    val tmpMap = HashMap<KEY, List<VALUE>>()
    for (value in values) {
        if (value != null) {
            val key = transformFunction.transform(value)
            var valuesList: List<VALUE>? = tmpMap[key]
            if (valuesList == null) {
                valuesList = ArrayList()
                tmpMap.put(key, valuesList)
            }
            valuesList.add(value)

        }
    }
    return Collections.unmodifiableMap(tmpMap)
}

The IDE now underlines the 'add' method here:

valuesList.add(value)

It says:

unresolved reference :add

How can i fix this?

UPDATE:

Here is my transform function:

    interface TransformFunction<VALUE, RESULT> {
    fun transform(value: VALUE?): RESULT
}

Sample usage:

    private fun getRSSIMultimap(rssiEvents: Collection<LocationRSSIEvent>): Map<NexoIdentifier, List<LocationRSSIEvent>> {
    return CollectionsUtils.createMultimap(rssiEvents, object : CollectionsUtils.TransformFunction<LocationRSSIEvent, NexoIdentifier> {
        override fun transform(locationRSSIEvent: LocationRSSIEvent): NexoIdentifier {
            return locationRSSIEvent.nexoIdentifier
        }
    })
}

1 Answer 1

10

Since you'r assigning a List<> to your valuesList it doesnt know add().

Change it to

 fun <KEY, VALUE> createMultimap(values: Collection<VALUE>, transformFunction: TransformFunction<VALUE, KEY>): Map<KEY, ArrayList<VALUE>> {
 var valuesList: ArrayList<VALUE>? = tmpMap[key]

Better use Kotlins MutableList which is currently the same as Javas ArrayList but may be changed in later versions to a native method.

 fun <KEY, VALUE> createMultimap(values: Collection<VALUE>, transformFunction: TransformFunction<VALUE, KEY>): Map<KEY, MutableList<VALUE>> {
    val tmpMap = HashMap<KEY, MutableList<VALUE>>()
    for (value in values) {
        if (value != null) {
            val key = transformFunction.transform(value)
            var valuesList: MutableList<VALUE>? = tmpMap[key]
            if (valuesList == null) {
                valuesList = mutableListOf()
                tmpMap.put(key, valuesList)
            }
            valuesList.add(value)

        }
    }
    return Collections.unmodifiableMap(tmpMap)
}
Sign up to request clarification or add additional context in comments.

8 Comments

Sorry, but it still underlines the 'add' method. I am using Android Studio 3.0. Maybe you can check it?
Please add your TransformFunction. It may be replaced with high order functions. Updated the code. Working now.
Your welcome. I highly recommend you to look at kotlinlang.org/docs/reference/lambdas.html since interfaces are not used in Kotlin.
Thanks, I am still try to learn, but not have much time, i refactor Java code :)
Can you look again on my updated question? I updated the sample usage - it underlines the 'object' property and 'override' overrides nothing
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.