Below are some examples of different ways to convert between arrays and their String
representations.
In the examples we’ll make use of a Person class that you can check in the GitHub repository, but you can use the same approach with different types. Assume that our input array is:
private final Person[] peopleArray = new Person[] {
new Person("Miss", "Jane", "Doe"),
new Person("Mr", "John", "Doe"),
new Person("Mr", "Name", "Surname")
};
How to convert an array to a String using Stream?
String resultString = Stream.of(peopleArray)
.map(Person::toString) // 1
.collect(Collectors.joining(", ")); // 2
System.out.println(resultString);
Output:
Miss. Jane Doe, Mr. John Doe, Mr. Name Surname
In this example, we have:
-
A
map
operation that transforms thePerson
object into aString
object using thetoString
method of thePerson
class, and finally, -
A
Collector
provided directly by Java that helps joining elements in theStream
using a given delimiter.
If you aren’t familiar with the ::
notation, this is used to indicate a method reference. In this case, we are asking the Stream
to map
each Person
object using the toString
method. We could express the same end result in different ways, for example:
String resultString = Stream.of(peopleArray)
.map(person -> {
return person.toString();
})
.collect(Collectors.joining(", "));
System.out.println(resultString);
This would yield the same output as before.
There are different variations of the Collectors.joining
method that allow for a prefix and suffix as well.
Converting an array to a String using a for-each loop
StringBuilder stringBuilder = new StringBuilder();
for (Person p : peopleArray) {
stringBuilder.append(p.toString() + "\n");
}
System.out.println(stringBuilder.toString());
Output:
Miss. Jane Doe
Mr. John Doe
Mr. Name Surname
One thing to note is that if we want to achieve the same format as with the Collectors
option we saw before, we need to add some additional checks to avoid including the comma separator after the last element.
We could do this using the following approach:
boolean firstElement = true;
StringBuilder stringBuilder = new StringBuilder();
for (Person p : peopleArray) {
if (firstElement) {
firstElement = false;
} else {
stringBuilder.append(", ");
}
stringBuilder.append(p.toString());
}
System.out.println(stringBuilder.toString());
Output:
Miss. Jane Doe, Mr. John Doe, Mr. Name Surname
Converting an array to a String using the Arrays class
String resultString = Arrays.toString(peopleArray);
System.out.println(resultString);
Output:
[Miss. Jane Doe, Mr. John Doe, Mr. Name Surname]
This option is the simplest one, but one that also provides less flexibility as you can’t alter the output format in the same way that the other options allow.
Converting a String into an array of Strings
In this example we’ll be covering String
splitting. The conversion of a String
into arrays of different types of objects will be covered in a later tutorial about marshalling/unmarshalling of data.
String inputString = "this,is,an,input,string";
String[] resultArray = inputString.split(",");
for (int i = 0; i < resultArray.length; i++) {
System.out.println(String.format("%d: %s", i, resultArray[i]));
}
Output:
0: this
1: is
2: an
3: input
4: string
In this case, we are making use of the String.split
method, indicating the comma as the delimiter. Note that the delimiter can be a regular expression.
The String.split
method has an overloaded version that allows specifying the maximum number of elements that should be returned.