org.apache.commons.beanutils.converters.ArrayConverter Example
This article introduces the ArrayConverter
class of the org.apache.commons.beanutils.converters
package and its basic usage. This is defined in the Apache Commons BeanUtils 1.9.2 API.
ArrayConverter
is generic Converter
implementation that handles conversion to and from array objects. The main features of this implementation are to convert:
- Individual elements of an array by delegating to a converter of appropriate type.
- From either arrays or collections to an array.
- To and from a delimited list in
String
format. - A
String
to multi-dimensional arrays.
The example programs are tested using Commons BeanUtils 1.9.2 on Windows 7 and Java SE 7. The following library files are required to compile and run the programs:
- Compile and run:
commons-beanutils-1.9.2.jar
- Run only:
commons-logging-1.2.jar
The converter library can be downloaded from Apache Commons BeanUtils website. Commons BeanUtils 1.9.2 API requires Java SE 5 or higher.
Examples
There are three example programs in this article. These programs show some of the features of array converter.
- The first program shows conversion of an array of objects to an array of strings. The input can be an array or a collection.
- The second program shows converting a delimited string to an array of elements.
- The third program shows conversion of an array to one delimited string.
1. Example 1
This example shows conversion of individual elements of an input array.
The input array has objects of Test
class. The program converts this to a String
array. This string array has elements representing the string representation of Test
.
1.1. The Test object to be converted
Create a Test
class whose instances are used in this example. Note the Object
class’s overridden toString()
method.
Test.java
public class Test { private String s; private int i; Test(String s, int i) { this.s = s; this.i = i; } @Override public String toString() { return (i + "." + s); } }
1.2. Build an array converter
The following code snippet shows the array converter is constructed.
StringConverter sConverter = new StringConverter(); ArrayConverter converter = new ArrayConverter(Test[].class, sConverter);
In the code snippet:
- A
StringConverter
is used to convert individual array elements. This is aConverter
implementation that converts an input object into aString
object. In this case theTest
class instance is to be converted to its string representation. - The
ArrayConverter
‘s constructor has two parameters. The first specifies the default array type this converter handles; theTest
array class. The second parameter specifies the component (or delegate) converter; in this case the string converter.
1.3. The input
Test [] input = {new Test("First", 1), new Test("Second", 2), new Test("Last", 9)};
The input is an array of Test
object elements that are to be converted. This has three elements.
NOTE: The input can also be a collection like a List
. In which case the code for this example remains same, except the input, which can be substituted with:
List input = new ArrayList(); input.add(new Test("First", 1)); input.add(new Test("Second", 2)); input.add(new Test("Last", 9));
1.4. Convert
String [] result = converter.convert(String[].class, input);
The convert()
method inherited from the Converter
interface is used to perform the conversion. The method takes two parameters. The first specifies the data type to which this value should be converted; in this case a String[]
. The second parameter specifies the input; in this case the array of Test
objects.
The resulting string array (String []
), has the converted elements.
1.5. The code
ArrayConverterExample1.java
import org.apache.commons.beanutils.converters.StringConverter; import org.apache.commons.beanutils.converters.ArrayConverter; public class ArrayConverterExample1 { public static void main (String [] args) { StringConverter sConverter = new StringConverter(); ArrayConverter converter = new ArrayConverter(Test[].class, sConverter); Test [] input = {new Test("First", 1), new Test("Second", 2), new Test("Last", 9)}; String [] result = converter.convert(String[].class, input); System.out.println("Converted value: "); for (String s : result) { System.out.println(s); } } }
1.6. The output
Converted value: 1.First 2.Second 9.Last
From the output the input array with three Test
objects:
Test [] input = {new Test("First", 1), new Test("Second", 2), new Test("Last", 9)};
is converted to a result array of Test
class’s string representation.
1.First 2.Second 9.Last
2. Example 2
This example shows conversion of an input string with delimiters to an array of elements with appropriate type. By default, this process uses a comma (,) as the delimiter. But, by using the setDelimiter()
method the default can be changed.
The example uses an input string delimited by colons (:) and converts it into a string array.
2.1. The code
ArrayConverterExample2.java
import org.apache.commons.beanutils.converters.StringConverter; import org.apache.commons.beanutils.converters.ArrayConverter; public class ArrayConverterExample2 { public static void main (String [] args) { StringConverter sConverter = new StringConverter(); ArrayConverter converter = new ArrayConverter(String[].class, sConverter); converter.setDelimiter(':'); String input = "1:2:3:4"; System.out.println("Convert string = " + input); String [] result = converter.convert(String[].class, input); System.out.println("Converted value: "); for (String s : result) { System.out.println(s); } } }
2.2. The output
Convert string = 1:2:3:4 Converted value: 1 2 3 4
From the output note that the delimited input string is converted to an array with four elements.
3. Example 3
This example converts an integer array to string – with all the array elements concatenated as a single delimited string value.
The program uses the setOnlyFirstToString()
method to specify conversion of all the elements in the array to a string. The method takes a boolean
parameter; true
, which is the default, converts only the first value in the array to a string. The program specifies false
.
3.1. The code
ArrayConverterExample3.java
import org.apache.commons.beanutils.converters.StringConverter; import org.apache.commons.beanutils.converters.ArrayConverter; public class ArrayConverterExample3 { public static void main (String [] args) { StringConverter sConverter = new StringConverter(); ArrayConverter converter = new ArrayConverter(int[].class, sConverter); converter.setOnlyFirstToString(false); int [] input = {1, 2, 3, 4}; System.out.println("Convert array: "); for (int i : input) { System.out.println(i); } String result = converter.convert(String.class, input); System.out.println("Converted value: "); System.out.println(result); } }
3.2. The output
Convert array: 1 2 3 4 Converted value: 1,2,3,4
From the output note that all elements of the input array are converted to one delimited string. The default comma (,) delimiter is seen in the output.
4. Download Java Source Code
This was an example of org.apache.commons.beanutils.converters.ArrayConverter
You can download the full source code of this example here: ArrayConverterExamples.zip