Convert a String to an InputStream in Java using Apache Commons IO

This code example shows how to convert a String to an InputStream using the Apache Commons IO library. Converting a String to an InputStream is sometimes necessary when for example we need to call some api that require a InputStream object as parameter but the value you need to pass is stored in a String object. To convert the parameter ‘name’ in the example below to an InputStream we simply use the IOUtils class which has a static method called ‘toInputStream’. The method takes the string we want to convert as parameter along with a character set and in the example we choose the common character set “UTF-8”.
Finally to make sure it has succeeded we print out every byte (character) in the stream on a separate row. Since the read-method of the inputstream returns an int, which is the decimal value of the byte, we convert the int value to a char before we print it out.

The output from the code above is:

Convert a String to an InputStream in Java

This code example shows how to convert a String to an InputStream. This is sometimes necessary when calling different api:s that require a InputStream object as parameter but the value you need to pass is stored in a String object. To convert the parameter ‘name’ in the example below to an InputStream we create a ByteArrayInputStream object which takes a byte array as parameter, so we simply call getBytes on the String we want to convert. To make sure it is in the proper format we also provide the character set UTF-8 to the getBytes method which we supply using a constant from the StandardCharsets class.
Finally to make sure it has succeeded we print out every byte (character) in the stream on a separate row. Since the read-method of the inputstream returns an int, which is the decimal value of the byte, we convert the int value to a char before we print it out.

The output from the code above will be:

Convert a String to int

The first and fastest way to convert a String to an integer value is to use the static method valueOf() of the Integer class. The valueOf() method can be used by other primitive wrapper classes too to convert between different types.

Another way is to use the static method parseInt of the Integer class to convert
a String to an int as below:

Continue reading

Convert list from one type to another using lambda expressions in Java

This example shows how to convert a list of a certain type of objects to a list with another type of objects using lambda expressions (available from Java 8).

First we create to classes to work with, Employee and Person. The employee has got three attributes and the Person class has just one that matches one the attributes from Employee, the name.

Then we create an ArrayList of employee objects and add one object to it. Finally we do the conversion like this:

Convert String[] array to boolean[] array using Apache Commons

This code example shows how to convert a string array to a boolean array. For this we use the Apache Commons Beanutils library that comes with quite a few converters for different data types. It also comes with a utility class containing methods to do conversions without directly using the converter for that specific type. That utility class is called ConvertUtils and the below example shows how to use it to convert a string array to a boolean array.

Continue reading