How to convert this line from java to kotlin


#1

I’ve got the following code snippet that I need converted to kotlin:

Map<Class<?>, Fetcher> fetchers = Maps.newHashMap();
fetchers.put(OrgEmployees::class.java, OrgEmployeesFetcher(...))
fetchers.put(OrgTesters::class.java, OrgTestersFetcher(...))

where Fetcher is an interface:

public interface Fetcher<I, O> {...}

and OrgEmployeesFetcher, OrgTesttersFetcher are implementations of this interface:

public class OrgEmployeesFetcher implements Fetcher<Object, OrgEmployees>{...}
public class OrgTestersFetcher implements Fetcher<Object, OrgTesters>{...}

I’m having trouble converting the first line: Map<Class<?>, Fetcher> fetchers = Maps.newHashMap();.


#2

try

val fetchers = mapOf(OrgEmployees::class.java to OrgEmployeesFetcher(...), 
                     OrgTesters::class.java to  OrgTestersFetcher(...))

and then use specifiy type explicitly IDE action if you really need type :slight_smile:


#3

Thanks that worked.
BTW, after doing this, intellij showed that the type I should have tried was:

HashMap<Class<out Any>, Fetcher<Any!, out Any!>>

(I used hashMapOf instead of mapOf).