Guava Splitter class demo

24 10 2015

Split String with a delimiter to obtain the array of parts – a common task in programming. Especially when you work on parsing files (like CSV), input streams, etc.

Let’s take the row of a sample csv file:

String csvRow = "desk,,chair,table,couch,";

Notice that it’s a bit tricky, since that particular row has second and the last column empty.

Source Code

if you want to run it on your own, get the source code from my github:

Java String.split()
Java’s split() works not intuitive, since the result of splitting:

String[] result = stringToSplit.split(",");


[desk, , chair, table, couch]

Notice that there is no last column. Java by default threw it out. I prefer such decisions to be left to the programmer. It is easier to filter out empty items than figure out if there were really just 5 columns or did Java cut out some of them…

Guava Splitter
Guava Splitter API is as simple as Java’s, but the implementation is different. To split String do:

List<String> result = Splitter.on(",").splitToList(stringToSplit);

As a result you get a List that is eagerly evaluated:

[desk, , chair, table, couch, ]

Notice that here you have the last empty string item present.

Guava Lazy Splitter
If you care about performance and lazy evaluation may be helpful in your case you can use the split() method instead of splitToList():

Iterable result = Splitter.on(",").split(stringToSplit);

The split() result of course contains exactly the same result as splitToList().

The result is a lazy evaluated Iterator instance. That means that at this point you actually don’t have your string splitted but just ready to be splitted. It will be splitted when you actually call anything on an iterator.

I don’t know the implementation details but I assume that the whole string will not be splitted at once, but it will be splitted step by step when you take the next() item from the iterator. So the real performance gain will be noticable when you do not iterate all items but break at some point.

Otherwise (if you iterate all items) the whole string will be splitted anyway (the difference is only ‘when‘ it will be evaluated).


If you have key-value map as a plain String like that:

String mapString = "office=desk,kitchen=table,living room=couch";

you can use MapSplitter class to convert it directly to Java Map:

Splitter.MapSplitter mapSplitter = Splitter.on(",").withKeyValueSeparator("=");
Map<String, String> result = mapSplitter.split(mapString);

And as a result you get the map:

{office=desk, kitchen=table, living room=couch}



Give Your feedback:

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: