I have a Map<Class,Object> .
In a method I transfer object. The method should compare the object with the classes from the Map and return the corresponding Object .
For example, in Map there are such classes: Number , String , Collection .
If you pass an object of class Boolean , it returns nothing.
If you pass an Integer , it will return an object corresponding to the Number class.
I tried this:
private static final Map<Class, Object> cache = new LinkedHashMap<>(); public Object getMapper(Object obj) { for (Iterator<Class> iterator = cache.keySet().iterator(); iterator.hasNext();) { Class key = iterator.next(); if (obj instanceof key) { return cache.get(key); } } return null; } But in if he does not see the key and offers to search for Maven. What is the problem?
instanceofrequires a type name, not a variable of typeClass. - VladD