Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

RefactoringNG: Bye bye, Hashtable

DZone's Guide to

RefactoringNG: Bye bye, Hashtable

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

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:

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}