Haiku for Java using Text Blocks
Creative writing meets Java Text Blocks and 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.
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:
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).
Characters using Java Streams
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
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.
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.
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.
Other Java Articles you may like
Top 5 Frameworks Java developers can learn for Microservices Development in 2022
Hello Java Programmers, Happy New Year 2022. If you are thinking about Microservice development in Java and looking for…
25 Essential skills Java Programmers and Architects can learn in 2022
22 essential tech skills professionals Java developers can learn in 2022 to further improve their profile and become…