Published in


Haiku for Java using Text Blocks

Creative writing meets Java Text Blocks and Eclipse Collections

Converting eleven haiku to a Java String using Text Blocks and counting characters using Eclipse Collections

My Month of Haiku

In September 2021, I took a month off from technical writing in my blog. I experimented with writing haiku for the month. Haiku are a structured form of poetry with a total of seventeen syllables, five in the first line, seven in the second line, and five in the third line.

Here’s a list of my haiku on Medium where I matched the haiku I wrote with selected images I found on Unsplash.


11 stories

Converting Haiku to a Java String using a Text Block

I organized the haiku into three rows with four, three and four haiku. The text block looks as follows:

Java Text Block with Haiku

Counting chars using Eclipse Collections

Once I had all of the haiku stored in a String, I wanted to see the top three letters I used in my writing. This was kind of inspired by the recent Wordle phenomenon. I was curious to see which letters I used most often in my own creative writing. The simplest approach for me would be to use Eclipse Collections.

The algorithm I used is pretty simple. Convert the String to a CharAdapter, filter out non-alphabetic characters, convert the remaining characters to lowercase, convert the characters to a CharBag (counts them), then ask the CharBag for the topOccurrences (top three letters).

Algorithm for counting top three letters using Eclipse Collections

Counting Characters using Java Streams

Counting Character instances is possible using Java Streams as well. The differences in approaches are subtle. There is no Bag data structure or topOccurrences algorithm in Java today, so I had to simulate a Bag using a Map and using sorting and limiting of the entrySet on the Map to calculate topOccurrences.

Algorithm for counting top three letters using Java Streams

Differences between Eclipse Collections and Java Streams

The differences between the Eclipse Collections and Java Streams approach are subtle. The Eclipse Collections version does not box char values as Character objects. Eclipse Collections also does not box the int counts as Long objects. A CharBag in Eclipse Collections is backed by a CharIntMap. The method topOccurrences can also handle ties (by leaving it to the developers to deal with ties), whereas the limit method will always limit the result to three.

The Haiku as Output

I added a System.out.println(haiku) in the code to see if the output of the text block was the same as the input looked, and it was.

All eleven Haiku as output from System.out.println()

The Source Code

I added some extra examples in the following gist. I calculate the distinct letters in all the haiku, as well as the duplicate and unique letters in all of the haiku. I solve these problems using Eclipse Collections and Java Streams.

Update: I added some tests to the source code below that find Wordle words (5 letters words, not contractions) in the haiku text block. I got some ideas and help from Stuart Marks, Vlad Zakharov, and José Paumard. During the discussion I learned about this great blog about Scanner from Stuart.

Source Code for HaikuTest

Final Thoughts

I had a nice break from technical writing in September 2021 and had fun writing haiku. I hadn’t had a real opportunity until now to use Java Text Blocks, and this turned out to be an interesting experiment.

I hope you enjoyed reading the blog!

I am a Project Lead and Committer for the Eclipse Collections OSS project at the Eclipse Foundation. Eclipse Collections is open for contributions. If you like the library, you can let us know by starring it on GitHub.




A humble place to learn Java and Programming better.

Recommended from Medium

Beginner's Guide to ROS — Part 1 “The Working Principle”

VR for the Web. Finally.

Data Factory & DataFlows

Trivy: Keep your artifacts vulnerability-free

Different ways to use kubernetes-pod-operator in Airflow

Agile Methodology from Zomato features

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Donald Raab

Donald Raab

Java Champion. Creator of the Eclipse Collections OSS Java library ( Inspired by Smalltalk. Opinions are my own.

More from Medium

Contract Test — Spring Cloud Contract vs PACT

Implementing Another In-Demand Spring Project — Spring Kafka

A simple mTLS guide for Spring Boot microservices

Implementing Reactive Circuit Breaker Using Resilience4j