Home
Map
Splitter Examples: split, splitToListUse the Guava Splitter class to split strings. Review splitToList and omitEmptyStrings.
Java
This page was last reviewed on Feb 23, 2023.
Splitter. Splitting strings is complicated. Often we want to transform strings as we split them (like with trimming). Sometimes Regex is not enough.
With Guava, a Java library, we use the Splitter class, and its "on" method for many advanced options. We can transform a String to a map of key-value pairs. We can ignore empty values.
SplitToList. This returns a List of Strings, which we can loop over or use in any other way. We call "on," a static method on the Splitter class, and specify the delimiter as an argument.
Info The on() method has many possible arguments. We can pass a CharMatcher or a regular expression pattern—or just a character.
Detail SplitToList returns a List of the strings. This can be transformed to an ArrayList or used directly in a loop.
import java.util.List; import com.google.common.base.Splitter; public class Program { public static void main(String[] args) { String value = "cup,tea,coffee"; // Use Splitter, on method, and splitToList. // ... Separates String on comma. List<String> list = Splitter.on(',').splitToList(value); for (String element : list) { System.out.println(element); } } }
cup tea coffee
TrimResults. Strings that have been split apart often need to be trimmed. They often have surrounding whitespace. With TrimResults, Splitter does this.
Tip TrimResults does not modify existing Strings. It takes effect before Strings exist—this can improve performance and reduce memory use.
Detail This example uses the Iterable result, which split() returns. This is easy to loop over, but is not a list collection.
import com.google.common.base.Splitter; public class Program { public static void main(String[] args) { String value = "rock ,stone, bird, fish"; // Split on comma, and trim our results. // ... Use split, which returns an Iterable. Iterable<String> result = Splitter.on(',').trimResults().split(value); for (String v : result) { System.out.println(v); } } }
rock stone bird fish
OmitEmptyStrings. Two delimiters sometimes occur right next to each other. This means an empty entry. But often in splitting, we don't want to keep empty entries.
Detail The result of the Splitter call is modified. So those empty entries are never added to our result list or Iterable.
import java.util.List; import com.google.common.base.Splitter; public class Program { public static void main(String[] args) { String values = "cat,,dog,,,fish"; // Omit empty Strings when splitting. List<String> list = Splitter.on(',').omitEmptyStrings() .splitToList(values); System.out.println(list.toString()); } }
[cat, dog, fish]
CharMatcher. The on() method can be passed a special class called a CharMatcher. We can access ready-built instances, like CharMatcher.WHITESPACE. This will match any whitespace.
So Using CharMatcher.WHITESPACE is a good way to split on any whitespace characters in the input string.
import java.util.List; import com.google.common.base.CharMatcher; import com.google.common.base.Splitter; public class Program { public static void main(String[] args) { String values = "hey, how\nare you\tdoing my friend?"; // Split on whitespace with CharMatcher class. List<String> results = Splitter.on(CharMatcher.WHITESPACE) .omitEmptyStrings().splitToList(values); for (String value : results) { System.out.println(value); } } }
hey, how are you doing my friend?
CharMatcher options. These are the options that can be passed to the Splitter.on method. Often we split on WHITESPACE to separate words.
ANY ASCII DIGIT INVISIBLE JAVA_DIGIT JAVA_ISO_CONTROL JAVA_LETTER JAVA_LETTER_OR_DIGIT JAVA_LOWER_CASE JAVA_UPPER_CASE NONE SINGLE_WIDTH WHITESPACE
CharMatcher.anyOf. We construct a CharMatcher with its static methods. Here we use anyOf to create a CharMatcher that will match any character with a String.
Detail We use a String as the CharSequence argument. Our matcher will split on comma, semicolon, and colon chars.
import java.util.List; import com.google.common.base.CharMatcher; import com.google.common.base.Splitter; public class Program { public static void main(String[] args) { String values = "one,two:three;four"; // Split with the CharMatcher.anyOf method. // ... We split on comma, semicolon and colon chars. List<String> results = Splitter.on(CharMatcher.anyOf(",;:")) .splitToList(values); for (String value : results) { System.out.println(value); } } }
one two three four
CharMatcher.inRange. Here is another way to use a CharMatcher with the Splitter.on method. We match a range of chars with inRange.
Tip The example will split on the chars 0, 1, 2 and 3 inclusive. It will not split on 4.
Result The entry "dog 4" is considered a valid string, unlike "cat 0." We have exact control over our delimiters.
import java.util.List; import com.google.common.base.CharMatcher; import com.google.common.base.Splitter; public class Program { public static void main(String[] args) { String values = "cat 0 dog 4 1 fish"; // Split on a range of characters. // ... We split on 0, 1, 2 and 3 but not 4. // ... We trim our resulting strings. List<String> results = Splitter.on(CharMatcher.inRange('0', '3')) .trimResults().splitToList(values); for (String value : results) { System.out.println(value); } } }
cat dog 4 fish
WithKeyValueSeparator. Sometimes keys and values are present in a String. With the withKeyValueSeparator method we transform these pairs into a Map.
Note We pass the separator within pairs to this method. The on() method receives the separator between pairs.
import java.util.Map; import java.util.Map.Entry; import com.google.common.base.Splitter; public class Program { public static void main(String[] args) { // This String contains key-value pairs. String value = "cat=Fluffy,dog=Spot,bird=Chirpy"; // Use Splitter to parse key-value separators. Map<String, String> map = Splitter.on(',').withKeyValueSeparator('=') .split(value); // Loop over entries. for (Entry<String, String> entry : map.entrySet()) { System.out.println("[" + entry.getKey() + "] " + entry.getValue()); } } }
[cat] Fluffy [dog] Spot [bird] Chirpy
HashMap example. HashMap is my favorite collection. We can convert the Map returned by Splitter by passing it to the HashMap constructor.
HashMap
import java.util.HashMap; import java.util.Map; import com.google.common.base.Splitter; public class Program { public static void main(String[] args) { String pairs = "cat:10 dog:4 fish:2"; // Separate pairs on spaces, and use colon as key-value separator. Map<String, String> map = Splitter.on(' ').withKeyValueSeparator(':') .split(pairs); // Create HashMap from Map. HashMap<String, String> hash = new HashMap<>(map); // Display some values. System.out.println(hash.get("cat")); System.out.println(hash.get("dog")); System.out.println(hash.get("fish")); } }
10 4 2
ArrayList example. Java programs extensively use the ArrayList collection. We can transform the List returned by splitToList to an ArrayList with the ArrayList's constructor.
ArrayList, String
Note For simple programs like this one, please consider using the split() method included with Java.
import java.util.ArrayList; import java.util.List; import com.google.common.base.Splitter; public class Program { public static void main(String[] args) { String input = "jeans/pants/shirt/socks"; // Use Splitter to get ArrayList of Strings. List<String> list = Splitter.on('/').splitToList(input); ArrayList<String> result = new ArrayList<>(list); System.out.println(result.toString()); } }
[jeans, pants, shirt, socks]
ToArray. Other parts of a program may require a String array. With Splitter, we can call splitToList and then invoke toArray. The syntax is a bit unusual.
Array
import com.google.common.base.Splitter; public class Program { public static void main(String[] args) { String data = "100:200:300:400"; // Convert result of splitToList to a String array. String[] result = new String[0]; result = Splitter.on(':').splitToList(data).toArray(result); // Display our results. for (String value : result) { System.out.println(value); } } }
100 200 300 400
Performance: Splitter versus String split. Is Splitter faster than Java's split? In this test, which just tests splitting on a comma char, I found Splitter is slightly slower.
However If a program requires trim() calls, or more advanced capabilities of Splitter, the results would likely be different.
And For a fast implementation of complex splits, a developer might end up with something resembling Splitter.
import com.google.common.base.Splitter; public class Program { public static void main(String[] args) { String value = "one,two,three,four,five,six"; int count1 = 0; int count2 = 0; long t1 = System.currentTimeMillis(); // Version 1: use Splitter from Guava. for (int i = 0; i < 1000000; i++) { Iterable<String> results = Splitter.on(',').split(value); for (String v : results) { count1 += v.length(); } } long t2 = System.currentTimeMillis(); // Version 2: use split from Java. for (int i = 0; i < 1000000; i++) { String[] results = value.split(","); for (String v : results) { count2 += v.length(); } } long t3 = System.currentTimeMillis(); // ... Counts. System.out.println(count1); System.out.println(count2); // ... Times. System.out.println(t2 - t1); System.out.println(t3 - t2); } }
22000000 22000000 389 ms: Splitter.on (Guava) 304 ms: split (Java)
With Splitter, from Guava, we simplify nontrivial splitting. For key-value pairs, or multiple-step splits where trimming or parsing is required, Splitter is a clear improvement.
C#VB.NETPythonGolangJavaSwiftRust
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Feb 23, 2023 (simplify).
Home
Changes
© 2007-2023 Sam Allen.