TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Splitter Examples: split, splitToList

Use the Guava Splitter class to split strings. Review splitToList and omitEmptyStrings.
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.guava: github.com
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.

On: This method has many possible arguments. We can pass a CharMatcher or a regular expression pattern—or just a character.

List: SplitToList returns a List of the strings. This can be transformed to an ArrayList or used directly in a loop.

Java program that uses Splitter, splitToList 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); } } } Output 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.

Iterable: This example uses the Iterable result, which split() returns. This is easy to loop over, but is not a list collection.

Java program that uses TrimResults, split 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); } } } Output 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.

With omitEmptyStrings: The result of the Splitter call is modified. So those empty entries are never added to our result list or Iterable.

Java program that uses omitEmptyStrings 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()); } } Output [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.

Java program that uses CharMatcher, Splitter 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); } } } Output 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.
CharMatcher options 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.

CharSequence: We use a String as the CharSequence argument. Our matcher will split on comma, semicolon, and colon chars.

Java program that uses CharMatcher.anyOf method 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); } } } Output 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.

Java program that uses CharMatcher.inRange 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); } } } Output 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.

Java program that uses withKeyValueSeparator 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()); } } } Output [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
Java program that uses Splitter with 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")); } } Output 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

Note: For simple programs like this one, please consider using the split() method included with Java.

Java program that uses Splitter with ArrayList 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()); } } Output [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
Java program that uses toArray, splitToList 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); } } } Output 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.

Java program that times Splitter, Java split method 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); } } Output 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.
It results in less work. It reduces the number of lines in some Java programs. And it makes them use more reliable, tested code—an obvious advantage.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf