2

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"); } } } 
13
  • 6
    code or it didn't happen Commented Sep 11, 2014 at 18:13
  • It should return true for both Commented Sep 11, 2014 at 18:14
  • 3
    can you check the import for your code if they are correct. Once i used apache map instead of java util ... Commented Sep 11, 2014 at 18:18
  • 3
    Then show us exactly your code that reproduces the problem, not just some gist like you have now in your question. By you by any chance make your own class named LinkedHashMap? If yes then remove it. Commented Sep 11, 2014 at 18:19
  • 2
    You will want to edit your current question with a full and complete example of the problem. Commented Sep 11, 2014 at 18:27

3 Answers 3

2

Running this code in a JUnit test and it passes, so they all return true.

Map<Long, String> map = new LinkedHashMap<Long, String>(); assertTrue(LinkedHashMap.class.isAssignableFrom(map.getClass())); assertTrue(map instanceof LinkedHashMap); assertTrue(HashMap.class.isAssignableFrom(map.getClass())); assertTrue(map instanceof HashMap); 
Sign up to request clarification or add additional context in comments.

Comments

1

But this code returns both true

As child is in fact an instance of Parent so obviously LinkedHashMap object will be an instance of its superclass HashMap and secondly LinkedHashMap will obviously be an instance of LinkedHashMap

final Map map = new LinkedHashMap(); if(map instanceof LinkedHashMap){ System.out.println("true"); } if(map instanceof HashMap){ System.out.println("true"); } 

Comments

1

The problem was that I changed the object type from HashMap to LinkedHashMap using hot deploy while debugging. Once I ran the same code after building again, I got the right behavior. Thanks for your answers. Original-

private final Map<Long, DD> cachedPlansById = new HashMap<Long, DD>(); 

New-

private final Map<Long, DD> cachedPlansById = new LinkedHashMap<Long, DD>(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.