Today I came across a very unexpected error, and while I was able to find a way to fix the problem as a whole, I'm not sure that I fully understand why she did what she did.
The code I'm working with was originally written using the JDK 7 JDK 7 environment. In the code I used ConcurrentHashMap and had to iterate over the keys on the map. For this, I used map.keySet() , which, according to JavaDocs, should have returned Set<K> . This worked fine until our build environment switched to JDK8.
When we switched to JDK8, I ensured that I would call the target / source for 1.7 when calling javac. Therefore, I was very surprised when the code started crashing when it wanted to iterate through the map keys. No error was selected, no exceptions, the thread just stopped. After some research, I found that the Java8 implementation for the ConcurrentHashMap the .keySet() method returns KeySetView<K,V> .
I fixed the problem by switching from using map.keySet() to getting Enumeration<K> using map.keys() .
Now I guess that although the project was compiled with Java7 targeting since JDK8 was used, the Java8 libraries were included, but why didn't it cause an error or exception when it got into a mismatch?
As the code snippet is set here:
class MapProcessing { private ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<String, Object>(); public MapProcessing() { map.put("First",new Object()); map.put("Second",new Object()); map.put("Third",new Object()); } public void processing() {
We compile using Oracle JDK 8 build 40, using target for 1.7 and source 1.7 in javac on a Windows 2012 server.
This code works using JVM 7 build 25 running on a Windows 2012 server.
java iteration concurrenthashmap
Jrsofty
source share