Friday, March 2, 2012

Useful Lesser Known Java Classes

A recent reddit Java thread is titled "Share a useful class from the standard Java Class Library!" and starts with the comment, "There are so many available classes and sometimes ones exist that you don't realize. Share one that you use that the rest of us may not be aware of!" In this post, I look at some of the (mostly JDK) classes mentioned in the forty (at time of this writing) responses to this request.

Several respondents provided concurrency-related Java classes such as Executors, java.util.concurrent.CountDownLatch, java.util.concurrent.atomic.AtomicInteger, ThreadLocal, and "anything in the packages" java.util.concurrent and java.util.concurrent.atomic.

Several classes related to String manipulation are mentioned in this thread. These include StringBuffer, and StringBuilder. I have blogged about these String-related classes in the post String, StringBuffer, and StringBuilder: There Is A Performance Difference. Other String-related classes mentioned in this thread include java.util.StringTokenizer and Apache Commons' StringUtils (which is mentioned in my post Checking for Null or Empty or White Space Only String in Java). The java.util.Scanner class also receives mention for making text parsing easier.

In the area of user interface work, java.awt.geom package is mentioned as are java.awt.Desktop and javax.swing.SwingUtilities. The class java.awt.Point is highlighted with an explanation that concludes, "But any two int pairs work and easier to pass to/from a function (instead of an array or whatever)." I could have used it instead of my own nested Coordinate class or instead of the JavaFX javafx.util.Pair class when crafting a JavaFX Christmas tree. The class java.awt.Robot is also mentioned in the thread and I have discussed that class in the post Screen Snapshots with Java's Robot.

Not surprisingly, several Java collections make the list. These include java.util.EnumSet and EnumMap (see my post The Sleek EnumMap and EnumSet), java.util.ArrayDeque (see my post The Java SE 6 Deque), java.util.PriorityQueue, java.util.Arrays, and java.util.Collections (see my post The Java Collections Class).

In my opinion, java.lang.ClassLoader, java.util.ServiceLoader, and java.nio.file.FileVisitor are some of the more elaborate and specialized classes mentioned in the reddit Java thread. Many of us use strong references by default, but java.lang.ref.WeakReference and java.lang.ref.SoftReference receive mention in this thread as well.

Finally, a class and a method that are referenced that I often use are the BigDecimal class (one of my many posts alluding to this class is Caution: Double to BigDecimal in Java) and the method System.nanoTime().

Conclusion

The reddit/Java thread Share a useful class from the standard Java Class Library! represents ideas from the Java community regarding lesser known useful Java classes generally available in the JDK. I liked many on the list and can think of additional examples as well (and likely more will appear from other posters). In particular, I think JDK 7's Objects class is mighty useful and less known than it should be. I also agreed with one of the responses which stated, "Add the google guava library for tons of useful classes" and have written several posts on Guava.

No comments: