First Look at Commons Lang 3.0 Beta

DZone 's Guide to

First Look at Commons Lang 3.0 Beta

· Java Zone ·
Free Resource

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.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}