I'm creating a LinkedHashMap using the following code-
private final Map<Long, DD> cachedPlansById=new LinkedHashMap<Long, DD>(); cachedPlansById instanceof LinkedHashMap But I I'm getting a 'true' on instanceof HashMap and a 'false' on instanceof LinkedHashMap. Can you please suggest how to make sure it is true for instanceof LinkedHashMap?
I can't declare it as LinkedHashMap, that breaks the code.
It's true that when I write a new test class, the above code works. Not sure what's the problem in my original class, since there are not even any imports to HashMap and cachedPlansById only gets instantiated once, so not sure how it gets shadowed.
Here's the test class:
import java.util.*; public class MapTest { public static void main(String[] args) { Map<Long, String> m = new LinkedHashMap<Long, String>(); if (m instanceof LinkedHashMap) { System.out.println("true"); LinkedHashMap<Long, String> l = (LinkedHashMap) m; if (l instanceof LinkedHashMap) System.out.println("true"); } } }
LinkedHashMap? If yes then remove it.