Gamer1120 Gamer1120 - 3 months ago 16
Java Question

How to use Java 7 HashMaps in Java 8?

I'm in the process of upgrading an application that relies on the Java 7 iteration order of HashMap. The Java 8 HashMap changed its iteration order, which is incompatible with some of this application (probably bad design), I have to use Java 7 HashMaps to successfully migrate the application to Java 8. This is the way I thought of doing it:


  1. Make a Java7HashMap that extends the regular Java 8 HashMap

  2. Look up the source code of the Java 7 HashMap, copy and paste.

  3. Replace the "import java.util.HashMap;" with "import (...).Java7HashMap;"

  4. Thoroughly test it all.



Would this be a good way of doing it? It is unfortunately not an option to rewrite the parts that rely on the Java 7 version of the HashMap.

Answer

Yes, your approach is fine. Though everyone seems to have a feeling of inadequacy. However you want to have only implementation changes:

Map<Lorem, Ipso> goodMap = new HashMap<>();
HashMap<Foo, Bar> badMap = new HashMap<>();

So replace only new HashMap with new Java7HashMap finding all misfits, and allowing later consolidation, say with better keys using Comparable.

You probably need just to implement Map of java 8, and actually I would not even care about missing java 8 support, as it will be working as it did, and a further development should offer a replacement of Java7HashMap. Better invest time in rewrites. A carrot and stick approach.