Shekhar Gulati is a Java Consultant with over 5 years experience.He is currently working with Xebia India an Agile Software Development company.The postings on this site and on his blog are his own and do not necessarily represent opinion of his employer. His own blog is at and you can follow him on twitter here. Shekhar has posted 25 posts at DZone. View Full User Profile

First Look at Commons Lang 3.0 Beta

  • submit to reddit

Apache Commons Lang 3.0 Beta was released on 4th August and the project has finally moved to Java 5. The API is now generified, varags are used where applicable, and some features which are now supported by Java itself have been  removed. All of this means that Lang 3.0 is not backwards compatible with the 2.x versions. To make it workable with the previous versions, the package name has been changed to 'apache.commons.lang3', allowing Lang 3.0 to sit side-by-side with previous version of Lang without any side effects. You can download the Commons Lang 3.0 beta from this link.

In this article, I will walk you through some of the features that have been added or improved in 3.0 release.


The ArrayUtils class allows you to do operations on arrays, primitive arrays, and primitive wrapper arrays. This class exists since version 2.0 and in rhw 3.0 release it has been generified and a new method called toArray has been added. Lets take a look at its features:

  1. toArray method
    This is a new method added in release 3.0 which lets you create a type-safe generic array.This method is very useful because arrays cannot be created from a generic type. For more information on why arrays can't be created for a generic type, refer to this link. This method uses the fact that varags are implemented as arrays, so this methods take a varags of T type and returns an array of T.
    String[] array = ArrayUtils.toArray("shekhar", "gulati");
  2. Another use of toArray method is to create generic empty array.
           String[] array = ArrayUtils.<String> toArray();
  3. toMap method
    This method exists since the 2.x days and has been generified in 3.0 release.The result of generifying this method is that you get type safe maps, hence you avoid ClassCastExceptions. For example, in 2.x versions
    Map map = ArrayUtils.toMap(new String[][]{{"shekhar","gulati"}});
  4.  But in 3.0 version,    

    Map<String, Integer> map2 = ArrayUtils.<String,Integer>toMap(new Object[][]{{"shekhar",1}});
  5. Other methods like subarray,addAll, add, etc are also generified.Hence they are now typ-safe.


This class is a new class, added in the 3.0 version. This class allows you to do Null-safe operations on CharSequence. It has two methods:

  1. length method
    This method gives you the length of any CharSequence.
    CharSequenceUtils.length(CharBuffer.wrap("shekhar")); // return 7
    CharSequenceUtils.length("shekhar"); // return 7
    CharSequenceUtils.length(new StringBuilder("shekhar").append("gulati"));//return 13
  2. subSequence method
    This method returns a new CharSequence starting with char specified at the index.
    CharSequenceUtils.subSequence("test", 1);
    will return est



The package 'org.apache.commons.lang.enums' has been removed from release 3.0 because from version 5 of Java, Enums are provided out of the box. A new class EnumUtils has been added which provides helper methods for Java enums. Lets take a look at some of its methods:

  1. getEnumMap method
    This method gives you a map of enums names and enums.
    enum ReportType{
    Map<String, ReportType> enumMap = EnumUtils.getEnumMap(ReportType.class);
  2. getEnumList method
    This method gives you a list of enums for a given enum class.
    List<ReportType> enumList = EnumUtils.getEnumList(ReportType.class);
  3. isValidEnum method
    This method checks if the given enum name is valid for given enum class.
    EnumUtils.isValidEnum(ReportType.class, "REPORT1");// will return true
    EnumUtils.isValidEnum(ReportType.class, "REPORT3"); // return false


ObjectUtils class methods like min,max, clone etc are generified which makes them type-safe. A new method firstNonNull(T...) is added. This method returns the first value in the array which is not null.

ObjectUtils.firstNonNull(null, null,null,null, "abc", null,"def");//will return "abc"


StringUtils does not need any introduction. I think every Java developer knows it. Some method signatures have changed to support CharSequence instead of String. For example, isEmpty(String) is changed to isEmpty(CharSequence). Some new methods have also been added :

  1. stripAccents method
    This method removes the accents from the string. This method works with JDK 1.6 method, and will fail on 1.5 version. 
    StringUtils.stripAccents("&ecute;clair") ;  //will return "eclair"
  2. normalizeSpace method
    This function returns the input string with whitespace normalized by using trim(String) to remove leading and trailing whitespace and then replacing sequences of whitespace characters by a single space.
    StringUtils.normalizeSpace("  a  b   c  ") ; // will return "a b c"
These are some of the features that have been either added or improved in 3.0 beta release. I will talk about other new features in second part of this series on Commons-Lang 3.0. Some of other new features are related to concurrency support and pluggable API for text transformation.
Published at DZone with permission of its author, Shekhar Gulati.

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


Eyal Golan replied on Tue, 2010/08/10 - 5:58am

do you know if they already made a maven repository?

Joerg Wassmer replied on Tue, 2010/08/10 - 8:14am

"normalizeSpace" should by called "collapse" or alike. "Normalizing" usually is only the task where the string is getting trimmed, and where each whitespace and control character is getting replaced by a space. Normalizing plus the replacement of subsequent whitespaces by a single space is called "collapsing". See javax.xml.bind.annotation.adapters.CollapsedStringAdapter and NormalizedStringAdapter.

Shekhar Gulati replied on Wed, 2010/08/11 - 10:01pm in response to:

Hello Eyal..I was also not able to find the maven repository..

James Carman replied on Wed, 2010/08/25 - 5:18pm

The toMap() method has been reverted due to lack of consensus that it should be generified in the way that it was generified.  We couldn't guarantee that the objects were of the correct type due to runtime type erasure.  So, it was reverted.  It did make code a bit more readable, but it did not guarantee type safety.  If you like the way it was generified, I urge you to send a message to asking that it be put back (make sure you put [lang] in the subject line).

Shekhar Gulati replied on Thu, 2010/08/26 - 8:20am in response to: James Carman

Hi James, I was the one who mailed the commons lang team to generify the toMap method. Should I mail it again? Thanks Shekhar

James Carman replied on Mon, 2010/11/29 - 5:53pm


 Sorry, I didn't get notified that you replied to this thread.  I would say, yes.  By all means, please do ask for it to be done again.  I liked the way it was generified.  I don't think the users were any worse off with the way I set it up.  They still have the runtime ClassCastException possibility either way, but if they're not doing something stupid, then they're okay and they don't need to do any casts.





Comment viewing options

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