gCaCBeBrAAfCXB-~~| 495VC~ 46577V~ 4657VC 49VP-~ 495VCC 4955VC 6465756VB 49G7684VBC 497VB 4G57476VCC 4959566}55X

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 `https://github.com/google/guava`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.`TrimResults.` Strings that have been split apart often need to be trimmed. They often have surrounding whitespace. With TrimResults, Splitter does this. `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.`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.`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.`CharMatcher options.` These are the options that can be passed to the Splitter.on method. Often we split on WHITESPACE to separate words. `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.`CharMatcher.inRange.` Here is another way to use a CharMatcher with the Splitter.on method. We match a range of chars with inRange. `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.`WithKeyValueSeparator.` Sometimes keys and values are present in a String. With the withKeyValueSeparator method we transform these pairs into a Map. `We pass the separator within pairs to this method. The on() method receives the separator between pairs.`HashMap example.` HashMap is my favorite collection. We can convert the Map returned by Splitter by passing it to the HashMap constructor. `HashMap `hashmap-java`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 `arraylist-java`For simple programs like this one, please consider using the split() method included with Java.`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. `Arrays `array-java`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.`For a fast implementation of complex splits, a developer might end up with something resembling Splitter.`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.

XqQzZQQq;java.util.qo;Qq;com.google.common.base.qdterqbqBqJqA {QXqBq?qcmain(qO[] args) {QQXXqO qhqyZzcup,tea,coffeezZ;ZQQXXq{Use qdter, on mqg,qVsplitToqo.QXXq9Separates qO on comma.QXXZqo<qO> qnqyZqdterZ.on(Z','Z).splitToqo(qh);QXXqw(qO q8 : qn) {QXXXqQ.out.q[ln(q8);QXX}QX}Q}QQZQQcupQteaQcoffeeZQQq;com.google.common.base.qdterqbqBqJqA {QXqBq?qcmain(qO[] args) {QQXXqO qhqyZzrock ,stone, bird,XfishzZ;ZQXXq{qd on comma,qVtrim our qMs.QXXq9Use split, which qKs an Iterable.QXXZIterable<qO> qMqyqdter.on(Z','Z).ZtrimResultsZ().ZsplitZ(qh);QXXqw(qO v : qM) {QXXXqQ.out.q[ln(v);QXX}QX}Q}QQZQQrockQstoneQbirdQfishZQQq;java.util.qo;Qq;com.google.common.base.qdterqbqBqJqA {QXqBq?qcmain(qO[] args) {QQXXqO qhsqyZzcat,,dog,,,fishzZ;ZQXXq{Omit empty qOs when splitting.QXXZqo<qO> qnqyqdter.on(Z','Z).ZomitEmptyqOsZ()QXXXX.splitToqo(qhs);QXXqQ.out.q[ln(qn.toqO());QX}Q}QQZQQ[cat, dog, fish]ZQQq;java.util.qo;Qq;com.google.common.base.CharMatcher;Qq;com.google.common.base.qdterqbqBqJqA {QXqBq?qcmain(qO[] args) {QQXXqO qhsqyZzhey, how\nare you\tdoing my friend?zZ;ZQQXXq{qd on whitespace with CharMatcher class.QXXZqo<qO> qMsqyqdter.on(ZCharMatcher.WHITESPACEZ)QXXXX.omitEmptyqOs().splitToqo(qhs);QXXqw(qO qh : qMs) {QXXXqQ.out.q[ln(qh);QXX}QX}Q}QQZQQhey,QhowQareQyouQdoingQmyQfriend?ZCharMatcher optionsZQQANYQASCIIQDIGITQINVISIBLEQJAVA_DIGITQJAVA_ISO_CONTROLQJAVA_LETTERQJAVA_LETTER_OR_DIGITQJAVA_LOWER_CASEQJAVA_UPPER_CASEQNONEQSINGLE_WIDTHQWHITESPACEZQQq;java.util.qo;Qq;com.google.common.base.CharMatcher;Qq;com.google.common.base.qdterqbqBqJqA {QXqBq?qcmain(qO[] args) {QQXXqO qhsqyZzone,two:three;fourzZ;ZQQXXq{qd with the CharMatcher.anyOf mqg.QXXq9We split on comma, semicolonqVcolon chars.QXXZqo<qO> qMsqyqdter.on(CharMatcher.ZanyOfZ(Zz,;:zZ))QXXXX.splitToqo(qhs);QXXqw(qO qh : qMs) {QXXXqQ.out.q[ln(qh);QXX}QX}Q}QQZQQoneQtwoQthreeQfourZQQq;java.util.qo;Qq;com.google.common.base.CharMatcher;Qq;com.google.common.base.qdterqbqBqJqA {QXqBq?qcmain(qO[] args) {QQXXqO qhsqyZzcat 0 dog 4 1 fishzZ;ZQQXXq{qd on a range of cq/s.QXXq9We split on 0, 1, 2qV3 but not 4.QXXq9We trim our qMing qPs.QXXZqo<qO> qMsqyqdter.on(CharMatcher.ZinRangeZ(Z'0'Z, Z'3'Z))QXXXX.trimResults().splitToqo(qhs);QXXqw(qO qh : qMs) {QXXXqQ.out.q[ln(qh);QXX}QX}Q}QQZQQcatQdog 4QfishZQQq;java.util.Map;Qq;java.util.Map.Entry;Qq;com.google.common.base.qdterqbqBqJqA {QXqBq?qcmain(qO[] args) {ZQQXXq{This qO cq: key-qh pairs.QXXZqO qhqyZzcat=Fluffy,dog=Spot,bird=ChirpyzZ;ZQQXXq{Use qdterqjparse key-qh separators.QXXZMap<qO, qO> mapqyqdter.on(Z','Z).ZwithKeyqXSeparatorZ(Z'='Z)QXXXX.split(qh);ZQXXq{qWover entries.QXXZqw(Entry<qO, qO> entry : map.entrySet()) {QXXXqQ.out.q[ln(z[zq}entry.getKey()q}z] zq}entry.getqX());QXX}QX}Q}QQZQQ[cat] FluffyQ[dog] SpotQ[bird] ChirpyZQQq;java.util.HashMap;Qq;java.util.Map;Qq;com.google.common.base.qdterqbqBqJqA {QXqBq?qcmain(qO[] args) {QQXXqO pairsqyZzcat:10 dog:4 fish:2zZ;ZQXXq{Separate pairs on spaces,qVuse colon as key-qh separator.QXXZMap<qO, qO> mapqyZqdterZ.on(' ').withKeyqXSeparator(':')QXXXX.split(pairs);ZQXXq{Cq_ HashMap from Map.QXXZHashMapZ<qO, qO> hashqyqqHashMap<>(map);ZQXXq{q= some qhs.QXXZqQ.out.q[ln(hash.get(ZzcatzZ));QXXqQ.out.q[ln(hash.get(ZzdogzZ));QXXqQ.out.q[ln(hash.get(ZzfishzZ));QX}Q}QQZQQ10Q4Q2ZQQq;java.util.qSqo;Qq;java.util.qo;Qq;com.google.common.base.qdterqbqBqJqA {QXqBq?qcmain(qO[] args) {QQXXqO inputqyZzjeans/pants/shirt/sockszZ;ZQQXXq{Use qdterqjget qSqo of qOs.QXXZqo<qO> qnqyZqdterZ.on('/').splitToqo(input);QXXZqSqoZ<qO> qMqyqqqSqo<>(qn);QXXqQ.out.q[ln(qM.toqO());QX}Q}QQZQQ[jeans, pants, shirt, socks]ZQQq;com.google.common.base.qdterqbqBqJqA {QXqBq?qcmain(qO[] args) {QQXXqO dataqyZz100:200:300:400zZ;ZQQXXq{CqN qM of splitToqoqja qO qU.QXXZqO[]Z qMqyqqqO[0];QXXqMqyZqdterZ.on(Z':'Z).splitToqo(data).ZtoqSZ(qM);ZQXXq{q= our qMs.QXXZqw(qO qh : qM) {QXXXqQ.out.q[ln(qh);QXX}QX}Q}QQZQQ100Q200Q300Q400ZQQq;com.google.common.base.qdterqbqBqJqA {QXqBq?qcmain(qO[] args) {QQXXqO qhqyZzone,two,three,four,five,sixzZ;QXXqicount1qy0;QXXqicount2qy0qbXXlong t1qyqQ.currentTimeMillis();ZQQXXq{Version 1: use qdter from Guava.QXXZqw(qiiqy0; i < 1000000; i++) {QXXXIterable<qO> qMsqyZqdterZ.on(Z','Z).split(qh);QXXXqw(qO v : qMs) {QXXXXcount1 += v.lq^();QXXX}QXX}QQXXlong t2qyqQ.currentTimeMillis();ZQQXXq{Version 2: use split from Java.QXXZqw(qiiqy0; i < 1000000; i++) {QXXXqO[] qMsqyqh.ZsplitZ(Zz,zZ);QXXXqw(qO v : qMs) {QXXXXcount2 += v.lq^();QXXX}QXX}QQXXlong t3qyqQ.currentTimeMillis();ZQQXXq9q]s.QXXZqQ.out.q[ln(count1);QXXqQ.out.q[ln(count2);ZQQXXq9Times.QXXZqQ.out.q[ln(t2 - t1);QXXqQ.out.q[ln(t3 - t2);QX}Q}QQZQQ22000000Q22000000QZ389 msZ:Xqdter.on (Guava)QZ304 msZ:XsplitX (Java)Z

+eb`ehceegdASplitter, splitToListTrimResults, splitomitEmptyStringsCharMatcher, SplitterCharMatcher.anyOf methodCharMatcher.inRangewithKeyValueSeparatorSplitter with HashMapSplitter with ArrayListtoArray, splitToListtimes Splitter, Java split method