As a Java programmer around the turn of the century I used Hashtable all over the place. It should now be considered legacy code and be replaced for instance by HashMap when you are upgrading old code. This post will cover how to get rid of Hashtable, some warnings, and simplify the code.

Opening a Java file from 2002 I find code similar to the lines below.

Pasting the legacy code in to JShell I get the following output.

It works as expected but it gives us a couple of warnings. Your IDE would give you comparable warnings. The warnings are related to that Hashtable is now supporting generics and we are expected to supply the types that will be stored in the collection. You can read more about that here:

https://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html

We could get rid of the warnings by updating line 1 to the following: Hashtable <String, String>cache = new Hashtable<>();. We could then also get rid of the typecasting in line 6.

However, when you are touching the old code you should rather move to the more modern HashMap class. The new class has better performance and current developers will recognize it 😊.

Suggested code follows: