Over a million developers have joined DZone.

RefactoringNG: Bye bye, Hashtable

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

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:

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}