Make or append me a String

Donald Raab
Javarevisited
Published in
5 min readNov 14, 2022

Learn how to use makeString and appendString in Eclipse Collections

Photo by That's Her Business on Unsplash

Learn to Kata and Kata to Learn

Three years ago I wrote an article for the book 97 Things Every Java Programmer Should Know with the title “Learn to Kata and Kata to Learn”.

In the article, I demonstrated how to create a kata exercise for joining Strings in Java. In the article, I iterated through several solutions, refactoring as I went along. I finally deleted all of my solutions, leaving only the kata, which could be solved at a later date.

Today is that date. I’m going to solve the kata now using Eclipse Collections. I will modify the kata slightly to be more festive for the fall season in the eastern United States.

A Fall inspired joinStrings Kata

Here is the modified test for the kata I will solve. I’ve used some emojis that remind me of the Fall season.

public void joinStrings()
{
List<String> fall = Arrays.asList("🎃", "🍎", "🍁", "🌽");
// Join the names and separate them by ", "
String joined = null;
Assertions.assertEquals("🎃, 🍎, 🍁, 🌽", joined);
}

This test currently fails. I‘ve used the StringJoiner, Java Stream and String.join solutions described in the article to solve this problem previously, but I have not shown how to use Eclipse Collections makeString or appendString methods to solve this problem.

Kata Solution with makeString

Here is the solution to the kata exercise using makeString.

public void joinStrings()
{
ImmutableList<?> fall = Lists.immutable.of("🎃", "🍎", "🍁", "🌽");
// Join the names and separate them by ", "
String joined = fall.makeString();
Assertions.assertEquals("🎃, 🍎, 🍁, 🌽", joined);
}

There are four overloaded forms of makeString. The simplest form takes no parameters, and will use “, “ as a default separator. That is what I have used above, as that is what the use test case required. There is a second form which takes a separator as a parameter, which I will demonstrate below.

public void joinStrings()
{
ImmutableList<?> fall = Lists.immutable.of("🎃", "🍎", "🍁", "🌽");
// Join the names and separate them by ", "
String joined = fall.makeString(", ");
Assertions.assertEquals("🎃, 🍎, 🍁, 🌽", joined);
}

The third form of makeString takes three Strings as parameters. The first is a starting String, the second is the separator and the third is an ending String. I will modify the test slightly to show the start and end Strings.

public void joinStrings()
{
ImmutableList<?> fall = Lists.immutable.of("🎃", "🍎", "🍁", "🌽");
// Join the names and separate them by ", "
// Starting with an "[" and end with a "]"
String joined = fall.makeString("[", ", ", "]");
Assertions.assertEquals("[🎃, 🍎, 🍁, 🌽]", joined);
}

These are three possible solutions to the kata using makeString.

Fall Fusion

The fourth overloaded form of makeString is actually a fusion of collect and makeString together. This form of makeString takes a Function as a first parameter, which will be applied to each element of the collection before. The three remaining parameters are start, separator, and end Strings. Here’s an example of the fourth form of makeString.

public void zipMakeString()
{
var fall1 = Lists.mutable.with("🎃", "🍎", "🍁", "🌽");
var fall2 = Lists.mutable.with("🥧", "🥧", "🍂", "🍞");
var zipped = fall1.zip(fall2);
String fusion = zipped.makeString(
pair -> pair.getOne() + pair.getTwo(),
"[",
" + ",
"] = Fall Fusion");
Assertions.assertEquals(
"[🎃🥧 + 🍎🥧 + 🍁🍂 + 🌽🍞] = Fall Fusion",
fusion);
}

In this example, I zip two lists together, and then print out the pairs of elements together using the Function specified as a lambda (pair -> pair.getOne() + pair.getTwo()) .

Kata Solution with appendString

Here is the solution to the kata exercise using appendString.

public void joinStrings()
{
ImmutableList<?> fall = Lists.immutable.of("🎃", "🍎", "🍁", "🌽");
// Join the names and separate them by ", "
StringBuilder builder = new StringBuilder();
fall.appendString(builder);
Assertions.assertEquals("🎃, 🍎, 🍁, 🌽", builder.toString());
}

Unlike makeString, appendString only has three overloaded forms. They match the first three forms of makeString, with the additional required parameter of an Appendable. Appendable is an interface in the JDK with several implementations, including StringBuilder.

I wrote a blog on Exception Handling in Eclipse Collections that included some code examples using the Appendable interface.

Why use ? instead of String

If you noticed in the Eclipse Collections examples, I used ImmutableList<?> instead of ImmutableList<String>. This was intentional. Both makeString and appendString will work with any type, not just String. You do not have to convert a type to a String first. This is not the case for Java Streams, StringJoiner and String.join which I used in the original article. For example, if I use ? as the type with String.join, the code will not compile.

public void joinStrings()
{
List<?> fall = Arrays.asList("🎃", "🍎", "🍁", "🌽");
// Join the names and separate them by ", "
String joined = String.join(", ", fall);
Assertions.assertEquals("🎃, 🍎, 🍁, 🌽", joined);
}

This code will result in the following compilation error.

In Eclipse Collections makeString and appendString both call toString on every object in the collection, so you don’t have to convert them first, if toString is a good enough method. Calling toString on a String in Java is a no op, and simply returns this.

Happy Fall!

I hope you enjoyed this blog and learned a bit about makeString and appendString. Both methods are available on the RichIterable and PrimitiveIterable interfaces in Eclipse Collections. This means most types included in Eclipse Collections support these methods.

Thanks for reading! Enjoy the rest of the Fall season!

I am the creator of and a Committer for the Eclipse Collections OSS project which is managed at the Eclipse Foundation. Eclipse Collections is open for contributions.

--

--

Donald Raab
Javarevisited

Java Champion. Creator of the Eclipse Collections OSS Java library (https://github.com/eclipse/eclipse-collections). Inspired by Smalltalk. Opinions are my own.