Guava Journey (1): Getting to Know Guava for the First Time, Basic Functions-Joiner

What is Guava? Many friends know it, and many others don't know it very well.

I will nag a few more words here and talk about the source of Guava.

Guava in Chinese means: Guava. Friends in the industry call him: Guava

Guava originated from Google's "Google Collections Library" project, and has been extended on its basis, involving current Java strings, collections, concurrency, I/O, and reflection, providing a large number of tools and methods that the Java API does not provide. Many friends have used various tools of commons under apache, such as StringUtils. Guava is similar to these tools.

Guava's APIs are used by many developers and are unit tested. Its reliability and performance are guaranteed.

Download address of Guava: https://code.google. com/p/guava-libraries/

I am using the version of Guava 17.0 here. Put the downloaded guava-17.0.jar on your classpath and you can use it. If Maven is used to manage jar dependencies, Maven's pom.xml is as follows:

<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>17.0</version>
</dependency>

 

Let's step into the topic, the target toolbar of this summary: Joiner

Joiner is a common tool class used to join the classes that implement the Iterable interface by specifying characters or strings. It provides a large number of static methods to achieve this goal. Refer to the code below:

First give a public class: Util.java. I use this utility class in my program to clearly distinguish the boundaries of the printed results.

import com.google.common.base.Strings;
public class Util {
    private Useful () {}
    public static void print() {
        System.out.println(Strings.repeat("=", 100));
    }
}

 The following is the use of Joiner, and the description of the API:

import static com.google.common.base.Joiner.on;

import org.kanpiaoxue.utils.Util;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class UsingTheJoinerClass_01 {

    public static void main(String[] args) throws Exception {
        UsingTheJoinerClass_01 obj = new UsingTheJoinerClass_01();
        obj. test();
    }

    /**
     * <pre>
     * Joiner can accept Array/Iterable/mutable variables as join parameters
     * Provided methods:
     * appendTo()
     * on()
     * join()
     * skipNulls ()
     * useForNull()
     * withKeyValueSeparator()
     * There are many overloaded versions of the above methods
     * </pre>
     */
    private void test() throws IOException {
        int count = 5;
        List<String> stirngList = createDemoList(count);
        // The basic usage of Joiner: use | for splicing, while ignoring the null value of null
        String rs1 = on('|').skipNulls().join(stirngList);
        System.out.println("Joiner base method: " + rs1);
        // output : Joiner base method: hello_1|hello_2|hello_4
        Util.print ();

        // Joiner uses the @ symbol for splicing, and assigns the value of "invalid value" to the null control
        rs1 = on('@').useForNull("invalid value").join(stirngList);
        System.out.println("join list:" + rs1);
        // output : join list:invalid value@hello_1@hello_2@invalid
        // value@hello_4
        Util.print ();

        String[] arr = { "1", "3", "4", "43", null, "55" };
        // Joiner's use of array array
        rs1 = on('=').skipNulls().join(arr);
        System.out.println("join array:" + rs1);
        // output : join array:1=3=4=43=55
        Util.print ();

        StringBuilder stringBuilder = new StringBuilder("===");
        // Joiner's use of StringBuilder
        rs1 = on("|").skipNulls().appendTo(stringBuilder, "foo", "bar", "baz").toString();
        System.out.println("join StringBuilder with some String:" + rs1);
        // output : join StringBuilder with some String:===foo|bar|baz
        Util.print ();

        StringBuilder stringBuilder1 = new StringBuilder();
        rs1 = on('*').skipNulls()
                .appendTo(stringBuilder1, new String[] { "hello", "world", null, "Green" })
                .toString();
        System.out.println("join StringBuilder with array:" + rs1);
        // output : join StringBuilder with array:hello*world*Green
        Util.print ();

        Map<String, String> testMap = Maps.newLinkedHashMap();
        testMap.put("Washington D.C", "Redskins");
        testMap.put("New York City", "Giants");
        testMap.put("Dallas", "Cowboys");
        // Joiner's use of Map
        String returnedString = on("#").withKeyValueSeparator("=").join(testMap);
        System.out.println(returnedString);
        // output : Washington D.C=Redskins#New York City=Giants#Dallas=Cowboys
    }

    /**
     * <pre>
     * @param count The number of elements to generate the List
     * @return List of strings
     * </pre>
     */
    private List<String> createDemoList(int count) {
        List<String> list = Lists.newArrayList();
        for (int i = 0; i < count; i++) {
            if (i % 3 == 0) {
                list.add(null);
            } else {
                list.add("hello_" + i);
            }
        }
        return list;
    }
}

 

 Note: Once an instance of Joiner is created, it is immutable, ie "thread safe".

 ============= End of the first section =================================

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326514814&siteId=291194637