My Top List of Java Tools

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/

0
Average: 4.5 (4 votes)

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

Comments

travisjwarren 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.

angiecltan 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

 

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 

Gramesmith1 replied on Wed, 2009/03/18 - 1:09am

I like some of my names for methods better, but there could be some ideas that I could steal from there too. custom logo design

___________________________


Company Logo Design | Label Design

trust234 replied on Tue, 2009/04/14 - 3:37am in response to: jari

What a great collection of tools.These are the tools that made it into my tricks box for everyday tasks. Tiresome operations are not my thing.Thanks for sharing. Travel to Russia

fastner123 replied on Sat, 2009/04/18 - 8:23am in response to: jari

I was hardly searching for these Java tools.I like to share in my Apex Professionals design.These are the tools that made it into my tricks box for everyday tasks. Tiresome operations are not my thing.

monicaw replied on Sun, 2009/04/19 - 2:55pm in response to: jari

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. ;-)

Meeting rooms heathrow

hanky replied on Mon, 2009/04/20 - 4:31am in response to: Gramesmith1

What a great collection of tools.We need these tools since we rarely modify our ways.I like to share in Houston Limousine.Thanks for sharing.

gangster134 replied on Mon, 2009/04/20 - 7:55am

It already sounds like Java tools.What a great collection of tools.We need these tools in our project work that is undergoing.Thanks a lot for the packed.com post.

LogoGuru replied on Tue, 2009/04/21 - 2:38am

Thanks for sharing these tools. As a computer science student it's very useful to get tips from advanced programers like Ignacio. StringUtils is a tool I used a lot in the past.

Custom Logo Design

finerst replied on Tue, 2009/04/21 - 5:23am in response to: Gramesmith1

Great contribution.I was hardly trying for these tools on all the sites.The are very much helpful to me.Thanks a lot for the quality post.Acne treatments

p0 replied on Wed, 2009/04/22 - 1:01am

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.
e cigarettes

dragging replied on Wed, 2009/04/22 - 7:33am in response to: trust234

Great post on Java tools...This is the info I was hardly searching for.I require some more sites on this.......Thanks a lot for sharing.Dissertation Help

pepper456 replied on Wed, 2009/04/22 - 11:12pm in response to: LogoGuru

Thanks for sharing a wonderful post.....I required this tools very much.......I like to share 10% of it with my friends.....really fulfilled my requirement.......Thank you a lot.....Custom Research Paper

yenster replied on Thu, 2009/04/23 - 2:00am

What kind of java tools.Are you going to provide all the tools for free? or only a few of them.Mention the list of the java tools......Essay Writing

likeware replied on Thu, 2009/04/23 - 5:44am

Very good gatherings.I like to have some more tools.could you tell me the site.I need them as soon as possible.Thanks for sharing.Resume

e-cigarette replied on Wed, 2009/04/29 - 1:20am

That goods! I think i can use it !Thinks 

e-cigarette

briget replied on Mon, 2009/05/04 - 4:37am

Really a great providence of the java tools.I need some more of them.........It is a pleasure sharing.these are the tools that made it into my tricks box for everyday tasks. Tiresome operations are not my thing.Thanks for the great stuff. regards, calvin klein underwear sale

stae3 replied on Tue, 2009/05/05 - 3:32am

Really a great provision of the tools.In these are the tools that made it into my tricks box for everyday tasks. Tiresome operations are not my thing.Thanks for the awesome stuff. regards,hid kit

p0 replied on Sun, 2009/05/10 - 1:57am in response to: trust234

I agree with you. Tis is really awesome Dissertation and i appreciate the way it was written.

thanks to your team.

bata replied on Tue, 2009/05/12 - 1:38am

I like these tools.I really got what I needed much.Thanks for the great sharing.Good sharing in my Buy Essay.

bata replied on Tue, 2009/05/12 - 1:40am

Wow! these are the very great tools.This an amazing Custom Essay writings.I appreciate the perfomance.Thanks for the whole team.

date replied on Tue, 2009/05/12 - 11:42pm

These are very great tools that are provided.Really had a good sharing with.I was very much satisfied with this.Thanks for sharing. regards,Research Papers

date replied on Tue, 2009/05/12 - 11:55pm

Really rocking post.I was searching for these kind of posts from many days.Really got what I wanted.But some tools are missed.So please post those too.Thanks for sharing. regards,Term Papers

nastmeq replied on Thu, 2009/05/14 - 6:44am

I accept this post.Since all these tools helps me a lot.I really searching for these tools since from many days.I had a glad time with this post.I wanna share this with my friends who are in search of these. regards,Essay

emil300 replied on Fri, 2009/05/15 - 2:12pm in response to: pepper456

Really a great provision of the tools.In these are the tools that made it into my tricks box for everyday tasks. Tiresome operations are not my thing.Thanks for the awesome stuff. regards amsterdam guide

Comment viewing options

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