Ignacio has posted 8 posts at DZone. View Full User Profile

My Top List of Java Tools

10.30.2008
| 36526 views |
  • submit to reddit

Lack of imagination is one of our worst sins as software developers. We do the same things over and over again, but we rarely modify our ways: me at least. After some years, these are the tools that made it into my tricks box for everyday tasks. Tiresome operations are not my thing.

Chances are you are already using at least some of these, but here we go anyways:

StringUtils

The bread and butter of the commons-lang library, this utility class includes some methods that should seriously have been included in String long time ago.
StringUtils.isEmpty(null) && StringUtils.isEmpty(""); // true
StringUtils.isBlank(" \n\t"); // true
StringUtils.substringAfterLast("foo.bar.baz", "."); // "baz"
StringUtils.substringBeforeLast("foo.bar.baz", "."); // "foo.bar"
StringUtils.split("foo.bar.baz", '.'); // { "foo", "bar", "baz" }
StringUtils.split("foo, bar,baz", ", "); // { "foo", "bar", "baz" }
StringUtils.leftPad("1", 3, '0'); // "001"

IOUtils and FileUtils

A must-have for the rare occasions where you need to manipulate files by hand. Both are pretty much alike (FileUtils for File, IOUtils for InputStream and Reader classes) and come bundled in commons-io.
File file1;
File file2;
InputStream inputStream;
OutputStream outputStream;

// copy one file into another
FileUtils.copyFile(file1, file2);
IOUtils.copy(inputStream, outputStream);

// read a file into a String
String s1 = FileUtils.readFileToString(file1);
String s2 = IOUtils.toString(inputStream);

// read a file into a list of Strings, one item per line
List<String> l1 = FileUtils.readLines(file1);
List<String> l2 = IOUtils.readLines(inputStream);

// put this in your finally() clause after manipulating streams
IOUtils.closeQuietly(inputStream);

// return the list of xml and text files in the specified folder and any subfolders
Collection<File> c1 = FileUtils.listFiles(file1, { "xml", "txt" }, true);

// copy one folder and its contents into another
FileUtils.copyDirectoryToDirectory(file1, file2);

// delete one folder and its contents
FileUtils.deleteDirectory(file1);

Google collections

This is the best implementation of a collections extension that I know of. Some of these are shouting to be included in the JDK:
// create an ArrayList with three arguments
List<String> list = Lists.newArrayList("foo", "bar", "baz");

// notice that there is no generics or class cast,
// and still this line does not generate a warning.
Set<String> s = Sets.newConcurrentHashSet();

// intersect and union are basic features of a Set, if you ask me
Set<String> s = Sets.intersect(s1, s2);

// Example of multiple values in a Map
ListMultimap<String, Validator> validators = new ArrayListMultimap<String, Validator>();
validators.put("save", new RequiredValidator());
validators.put("save", new StringValidator());
validators.put("delete", new NumberValidator());

validators.get("save"); // { RequiredValidator, StringValidator }
validators.get("foo"); // empty List (not null)
validators.values(); // { RequiredValidator, StringValidator, NumberValidator }

java.util.concurrent

Not everybody needs the heavy lifting of java.util.concurrent, but the concurrent collections are handy:
// a map that may be modified (by the same or different thread) while being iterated
Map<String, Something> repository = new ConcurrentHashMap<String, Something>();

// same with lists. This one is only available with Java 6
List<Something> list = new CopyOnWriteArrayList<Something>();
Hardly a large toolbox, is it? If your favourite library is missing, feel free to add it below. :)

 

From http://icoloma.blogspot.com/

Published at DZone with permission of its author, Ignacio Coloma.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Travis Warren replied on Thu, 2008/10/30 - 7:29pm

Excellent list, agree 100% on StrigUtils, indispensable. 

 Also a big fan of commons lang ToString / Hash / Equals Builders.

 For date time functions, you can't go past JodaTime.

 Was not aware of the google collections, will check it out.

Angie Tan replied on Fri, 2008/10/31 - 5:48am

Although the list sounds more like Java libraries but I would have to agree with ya... Can't seem to live without StringUtils or for the matter, Apache's Common packages. ;-)

Talip Ozturk replied on Mon, 2008/11/03 - 8:33am

 

> // same with lists. This one is only available with Java 6

> List<Something> list = new CopyOnWriteArrayList<Something>();

 

CopyOnWriteArrayList seems to be also available with Java 5 (1.5)

 

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/CopyOnWriteArrayList.html

 

Good stuff,

-talip

Hazelcast: Distributed queue, map, set, list, lock and executorservice

 

Michael Munsey replied on Tue, 2008/11/04 - 2:17am

The pure Java Arrays.asList() seems to be the same as Lists.newArrayList()

  List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");

Apache collection MultiHashMap is similar to Google's ArrayListMultimap, although they are not exactly the same.

Dennis Cheung replied on Wed, 2008/11/12 - 9:34am

@Talip Ozturk

 It saved you duplicated code of the generic type.

 

@munsey

It is quite difference between a newArrayList() vs Arrays.asList()

Arrays.asList() is adapter pattern
>>Returns a fixed-size list backed by the specified array.

newArrayList() do return a real ArrayList like this
>>new ArrayList(Arrays.asList())

 

I heard that next version  of JDK will study from google collection, a some utility to use generic collection easier.

 

James Ervin replied on Fri, 2008/11/14 - 6:25pm

No joke, I cringe every time I see code where Jakarta Commons StringUtils would obviously apply.  As far as the collections goes.  I wrote an implementation like that myself, since Google's collections did not exist at that time.

I have it in a subversion repository at: http://svn.codehaus.org/groovy/trunk/groovy/ide/groovy-eclipse/org.codehaus.groovy.eclipse.collections

 Don't be fooled too much by the name, it is not by any means Eclipse specific.  It is there so that I could use it while working on the Groovy Eclipse plugin.  I like some of my names for methods better, but there could be some ideas that I could steal from there too.

Good work, in fact, if anyone tries anything from this list, go see org.apache.commons.lang.StringUtils, that class alone is worth the trip and download.  You will wonder how you got along without it.

 

James E. Ervin 

Jari Pakarinen replied on Sat, 2008/12/06 - 7:31am

Anyone know of .NET C# versions of these libraries ? Especially StringUtils, IO and File would be great. I have looked but have not found any.

Jari 

gfgfg jhgjg replied on Sat, 2009/05/23 - 6:33am

cool stuff

asd sdf replied on Fri, 2009/06/05 - 2:54am

cool

john green green replied on Sun, 2009/10/25 - 8:50am

It is there so that I could use it while working on the nike shoes chinaGroovy Eclipse plugin. I like some of my names for methods better, but there could be some ideas that I could steal from there too.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.