Useful snippets

Specific snippets for specific purposes.

org.apache.commons.lang.mutable.MutableInt: A mutable integer, useful for declaring final to pass into an anonymous inner class, such as during invokeAndWait(). Also useful if you need to pass by reference, such as in a swap operation.

java.util.concurrent.atomic.AtomicInteger: For shared access to an Integer, such as a website counter or to issue a unique sequence using incrementAndGet(). Better performance than synchronize methods/blocks.

java.util.concurrent.ConcurrentHashMap: Features of Hashtable at the performance of HashMap. Hashtable locks itself for all operations. synchronizedMap() wraps the operations so performance suffers as well.

java.math.BigDecimal: Use this to avoid the floating-point representation issue.

org.apache.commons.io.IOUtils.closeQuietly(stream): Use this in finally block to mute exceptions and null streams. Also useful in IOUtils are copy(), toByteArray() and toString() methods.

org.apache.commons.lang.StringUtils: Null-safe methods for basic String operations, as well as other useful stuff e.g. pad(), repeat(), right().

Joda Date: Easier to use date class than JDK Date/Calendar.

java.util.Arrays.toString(array): print array contents instead of getting [I@1e7ed86. Also useful are sort() and fill() method, e.g. false boolean array.

RSS feed for comments on this post · TrackBack URL

Leave a Comment