Although it’s both powerful and robust, Java’s nevertheless got a (probably well-deserved) reputation as a bit of a time-sink. Coding something in Java can be time-consuming, and many a Java developer can recall stints of sleeplessness slaving over a keyboard to code their apps. Then again, that’s not really limited solely to Java, is it?
Developers dedicate a lot of time to optimizing their workflows and seeking our shortcuts that improve productivity. In this article, I’d like to save devs some time and share my favorite Java productivity enhancers.
See Your Shortcuts In Eclipse
Here’s one for all you novice developers – we’ll start with something simple before we start getting into the real secrets. While programming in the Eclipse framework, you can press Ctrl+Shift+L to bring up a widget that displays every available shortcut key. Though this is a fairly simple shortcut – and perhaps not as unknown as some others on the list – it’s still a fantastic one to be aware of.
Grab The Stack Trace Of An Exception
Checking your code for errors is one of the most painstaking – and frustrating – parts of the development process. It’s also horrendously time-consuming, particularly if you’ve a pesky error whose source isn’t immediately clear. Using this code, you can more easily track down where an exception is occurring – and bring yourself that much closer to being rid of it. (via Alethis)
Exception e = …;
java.io.StringWriter sw = new java.io.StringWriter();
String trace = sw.getBuffer().toString();
Merge Your Hash Tables
If you want to merge two hashtables, you could iterate through every value manually – though that method is horrifically inefficient. Here’s a simple, effective alternative that’ll allow you to merge your hashtables with minimal fuss. Again, this code is courtesy of Alethis.
Map m1 = …;
Map m2 = …;
m2.putAll(m1); // adds all elements of m1 to m2
Check If Your String Dates Are Valid
Let’s say you’ve got a string with a date in it, and you want to convert that string to an object. How can you ensure that this object will remain valid regardless of the way the date is formatted? This post by Viral Patel details a Util class that’ll allow you to quickly and easily do so.
Cut Down On String Size
If you’re looking to optimize Java’s performance, packing your strings is a great place to start – especially if you’re storing a great deal of them in memory. The Java Performance Tuning Guide provides a few great tactics for lowering the size of your strings – and increasing the performance of your installation. You can find them here and here, respectively.
Deal Better With Generics In Java 8
We’ll top things off with one of the lesser-known features of Java 8; a trick that’ll make dealing with generics a great deal easier for Java designers. Although it’s not a complete panacea, the shortcut allows for much simpler type inference management. You can read more about it here.