0

I have a Map like Map<Long, Long> serials I want to iterate it in a for loop. My code is

for (Map.Entry<Long, Long> serial : serials.entrySet()) {
   custOrder = get(serial.getKey());
}

Where the get() method take a Long.

It's compile with no error. But in run time serial.getKey() returns a String.

Md. Shougat Hossain
  • 501
  • 2
  • 8
  • 24
  • 1
    This makes no sense, if `serial.getKey()` returns a `String`, your code shouldn't pass compilation (unless you are using a raw `Map` type in the declaration of `serials`). – Eran Dec 10 '17 at 09:53
  • That means you used the map as a raw type somewhere in your code, or used reflection to populate it. Don't do that. Or at least, if using reflection, well, fix it. – JB Nizet Dec 10 '17 at 09:53
  • Where is your `Map serials` initialization? – Zico Dec 10 '17 at 09:53
  • How can you sure about "But in run time `serial.getKey()` returns a String."? – Zico Dec 10 '17 at 09:56
  • Paste the output of ```serial.getKey().getClass()``` – Sudheera Dec 10 '17 at 10:24
  • @JBNizet This code is in a method `saveVPSerials(Map serials)` where `serials` is a parameter. How can I do this without row map type – Md. Shougat Hossain Dec 10 '17 at 10:27
  • The problematic code (if the key is really a string), is elsewhere. You're putting strings somehow in this map. We can't explain with the code you posted. Read https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it – JB Nizet Dec 10 '17 at 10:38

0 Answers0