Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin unresolved reference :add after converting from Java code

Tags:

java

kotlin

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
        }
    })
}
like image 524
K.Os Avatar asked Oct 20 '17 13:10

K.Os


1 Answers

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)
}
like image 94
Emanuel S Avatar answered Sep 29 '22 18:09

Emanuel S