Google Guava String Splitter

Google Guava has a special place in my dark, cold heart; prior to Java 8, Google Guava has allowed some level of "functions as first class citizen" and lambda-like features making less verbose code with older Java codebases.

Changes since Java 8 has mostly nullified the need of the features of Google Guava. However, despite the advancements of its features and standard libraries, Java has yet to come up of a simple and robust-yet-intuitive-and-performant way of splitting strings into tokens. We have the String::split or the Pattern::splitAsStream which are serviceable but doesn't have a lot of options and prone to be written in a brittle way (NullPointerException and ArrayOutOfBoundsException, anyone?)

This is where Google Guava's Splitter comes in. It's very intuitive and has a lot of options to ensure robust code.

var tokens = Splitter.on('|')
  .trimResults()
  .omitEmptyStrings()
  .splitToList("foo|| bar");        

The above code will yield a list of strings containing "foo" and "bar".

If you have a comma-delimited key-value pairs it can parse that into a Map type too!

var kv = Splitter.on(",")
  .omitEmptyStrings()
  .trimResults()
  .withKeyValueSeparator(":")
  .split("a:100, ,,b:200");        

Above yields a map with key value pairs a:100 and b:200

Not only that: it's really fast. I've done some comparison between this and Java's splitter and Scala's splitter and Guava is faster on larger datasets.

The only thing that's not great about Splitter is that it doesn't have the convenience of splitting string into a Java stream, only Iterable or list. One could argue that it's an overhead when using it conjunction with Java streams, but in my real-world experience, this hasn't been the case.

Incidentally, there's a companion class Joiner that does the opposite: join strings with specified delimiter, although I haven't really used it that much.


要查看或添加评论,请登录

Dexter Legaspi, MSc的更多文章

社区洞察

其他会员也浏览了