Over a million developers have joined DZone.

RefactoringNG: Bye bye, Hashtable

DZone's Guide to

RefactoringNG: Bye bye, Hashtable

· Java Zone
Free Resource

Try Okta to add social login, MFA, and OpenID Connect support to your Java app in minutes. Create a free developer account today and never build auth again.

RefactoringNG (earlier article on DZone here) is a flexible and powerful Java refactoring tool, written as NetBeans module. Below you see how to use it when refactoring Hashtables.

The Hashtable class is a synchronized implementation of hash table and it is in the Java API since JDK 1.0. Since JDK 5, the Java API has also ConcurrentHashMap that is functionally equivalent to Hashtable but contrary to Hashtable enables concurrent operations. Concurrent operations result in better efficiency. So, it makes sense to "upgrade" the code from Hashtable to ConcurrentHashMap. In this article, we show how we can upgrade the code automatically.

We will use two rules. The first rule will replace fully qualified name java.util.Hashtable with java.util.concurrent.ConcurrentHashMap and the second rule will replace simple name Hashtable with ConcurrentHashMap.

// java.util.Hashtable -> java.util.concurrent.ConcurrentHashMap
 MemberSelect [identifier: "Hashtable"] {
    MemberSelect [identifier: "util"] {
       Identifier [name: "java"]
 } ->
 MemberSelect [identifier: "ConcurrentHashMap"] {
    MemberSelect [identifier: "concurrent"] {
       MemberSelect [identifier: "util"] {
          Identifier [name: "java"]
// Hashtable -> ConcurrentHashMap
 Identifier [elementKind: CLASS, qualifiedName: "java.util.Hashtable"] ->
 Identifier [name: "ConcurrentHashMap"]

If you want to try it, download the latest version of RefactoringNG and HashtableDemo.zip. Here are a few screenshots:

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}