1. 程式人生 > >guava原始碼閱讀之Joiner

guava原始碼閱讀之Joiner

建議:簡單看下appendTo即可 

Joiner 主要用來將字串或者字串陣列、列表之類的東西,拼接成一個以指定符號分隔各個元素的字串,比如把 [a, b, c] 拼接成 “a b c”。

舉個栗子:

String str1[] = {"a", "b", "c", "d"};
String str2[] = {"a", "b", "c", "d", null};
String temp = Joiner.on(",").join(str1);
LOGGER.info(temp);

temp = Joiner.on(',').skipNulls().join(str2);
LOGGER.info(temp);

temp = Joiner.on(',').useForNull("空").join(str2);
LOGGER.info(temp);

 

關於appendTo(A appendable, Iterator<?> parts)

public <A extends Appendable> A appendTo(A appendable, Iterator<?> parts) throws IOException {
    Preconditions.checkNotNull(appendable);
    if (parts.hasNext()) {
        appendable.append(this.toString(parts.next()));

        while(parts.hasNext()) {
            appendable.append(this.separator);
            appendable.append(this.toString(parts.next()));
        }
    }

    return appendable;
}

關於傳入的型別,貼個圖就明白了