JDK-StringJoiner構(gòu)造及添加元素源碼分析
背景
功能描述:
將多個元素使用指定符號前后連接為字符串;
eg:1 2 3 4 5 , => 1,2,3,4,5
要點(diǎn):
- 多個元素
- 指定分隔符
- 分隔符只在元素之間,不能作為第一或最后一個
使用方法:
// 1 構(gòu)造 設(shè)置分隔符/前綴/后綴
StringJoiner joiner = new StringJoiner(",", "start", "end");
// 2 添加元素
List<String> elements = Arrays.asList("1","2","3","4")
for (String s: elements) {
joiner.add(s);
}
// 3獲取拼接后結(jié)果
joiner.toString(); // "start1,2,3,4end"源代碼
基礎(chǔ)屬性
public final class StringJoiner {
// 前綴
private final String prefix;
// 分隔符
private final String delimiter;
// 后綴
private final String suffix;
// 拼接后的value,包含前綴、分隔符、和拼接元素,不包括后綴,后綴在toString返回前才會填充
/*
* StringBuilder value -- at any time, the characters constructed from the
* prefix, the added element separated by the delimiter, but without the
* suffix, so that we can more easily add elements without having to jigger
* the suffix each time.
*/
private StringBuilder value;
// 空值,默認(rèn)為prefix+suffix,支持自定義設(shè)置
private String emptyValue;
}
構(gòu)造方法
public StringJoiner(CharSequence delimiter) {
this(delimiter, "", "");
}
public StringJoiner(CharSequence delimiter,
CharSequence prefix,
CharSequence suffix) {
Objects.requireNonNull(prefix, "The prefix must not be null");
Objects.requireNonNull(delimiter, "The delimiter must not be null");
Objects.requireNonNull(suffix, "The suffix must not be null");
// make defensive copies of arguments
this.prefix = prefix.toString();
this.delimiter = delimiter.toString();
this.suffix = suffix.toString();
this.emptyValue = this.prefix + this.suffix;
}
可以看到,構(gòu)造方法只是簡單的根據(jù)參數(shù)設(shè)置響應(yīng)的屬性,
值得注意的是:
this.emptyValue = this.prefix + this.suffix;
默認(rèn)的 emptyValue 是由前綴和后綴簡單拼接而成?
添加元素
public StringJoiner add(CharSequence newElement) {
prepareBuilder().append(newElement);
return this;
}
private StringBuilder prepareBuilder() {
if (value != null) {
value.append(delimiter);
} else {
value = new StringBuilder().append(prefix);
}
return value;
}add 方法首先調(diào)用了 prepareBuilder 方法用來執(zhí)行添加元素的前置操作 ;
prepareBuilder 內(nèi)部會先判斷 value 是否為空,若為空則構(gòu)建StringBuilder對象且追加前綴,如果不為空則追加分隔符;
執(zhí)行邏輯如下:
joiner.add("1");
---
prepareBuilder() // value = "start"
.append("1"); // value = "start1"
=========================================================
joiner.add("2");
---
prepareBuilder() // value = "start1,"
.append("2"); // value = "start1,2"
=========================================================
joiner.add("3");
---
prepareBuilder() // value = "start1,2,"
.append("3"); // value = "start1,2,3"拼接結(jié)果
public String toString() {
// 沒有調(diào)用add方法添加元素,直接返回空值
if (value == null) {
return emptyValue;
} else {
// 沒有后綴,直接將value.toString()即可
if (suffix.equals("")) {
return value.toString();
} else {
// 追加后綴后返回
int initialLength = value.length();
String result = value.append(suffix).toString();
// reset value to pre-append initialLength
value.setLength(initialLength);
return result;
}
}
}
核心的邏輯的在:
String result = value.append(suffix).toString();
一句話描述該邏輯是:拼接后綴,然后把完整的結(jié)果返回;
疑問點(diǎn)
主要關(guān)注下追加后綴后得到結(jié)果的前后:
int initialLength = value.length(); // 獲取value長度 String result = value.append(suffix).toString(); // value中追加后綴,得到最終結(jié)果 value.setLength(initialLength); // 長度重置回追加后綴前的長度,可以理解為將原長度之后字符清空 return result; // 返回拼接完成的結(jié)果
- 獲取value的長度
- 添加后綴生成最終的結(jié)果result
- 將value長度重置回添加后綴之前
- 返回結(jié)果
只需要2就拿到結(jié)果了,為什么要1和3?如果把1和3去掉,會怎么樣?
StringJoiner joiner = new StringJoiner(",", "start", "end");
List<String> elements = Arrays.asList("1","2","3","4")
for (String s: elements) {
joiner.add(s);
}
joiner.toString(); // "start1,2,3,4end"
joiner.add("5");
joiner.toString(); // "start1,2,3,4end,5end"
在StringJoiner#toString方法中用于拿到完整結(jié)果臨時填充進(jìn)去后綴沒有被去掉,污染了后續(xù)使用joiner對象;
顯而易見,1&3步驟是為了清理臨時填充的后綴"end"。
清理怎么實(shí)現(xiàn)的? setLength設(shè)置長度就好了?
看下StringBuilder中的setLength的實(shí)現(xiàn):
//java.lang.AbstractStringBuilder#setLength
public void setLength(int newLength) {
if (newLength < 0)
throw new StringIndexOutOfBoundsException(newLength);
ensureCapacityInternal(newLength);
if (count < newLength) {
Arrays.fill(value, count, newLength, '\0');
}
// 設(shè)置count為填充后綴之前的長度值
count = newLength;
}
// java.lang.StringBuilder#toString
public String toString() {
// 使用value[0,count]位置的字符生成結(jié)果
// Create a copy, don't share the array
return new String(value, 0, count);
}可以看到,setLength方法是將原長度值賦值給count屬性;count是StringBuilder中維護(hù)用來記錄填充字符數(shù)量的屬性;
StringBuilder#append會從count的位置向后操作value數(shù)組;StringBuilder#toString方法調(diào)用時也會只會使用[0,count]位置的字符生成字符串結(jié)果;
至此核心源碼分析就結(jié)束了;
新姿勢
對于有狀態(tài)的對象,冪等方法內(nèi)如果要臨時改動內(nèi)部狀態(tài),邏輯完成后需要將變動的屬性恢復(fù)原貌;
對于使用者而言,StringJoiner#toString方法應(yīng)該是冪等的,不能對當(dāng)前象內(nèi)的數(shù)據(jù)狀態(tài)做任何變動,這樣才能保證得到的結(jié)果是一致的;
再回顧下StringJoiner#value上面的注釋:
at any time, the characters constructed from the prefix, the added element separated by the delimiter, but without the suffix
處理數(shù)組&列表等數(shù)據(jù)時,可以標(biāo)記刪除,性能更好;刪除數(shù)據(jù)時不一定要把指定位置數(shù)據(jù)清理掉,使用used_index標(biāo)記有效位置,刪除時修改下used_index的方法會更高效;
以上就是JDK-StringJoiner構(gòu)造及添加元素源碼分析的詳細(xì)內(nèi)容,更多關(guān)于JDK-StringJoiner源碼分析的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于Spring實(shí)現(xiàn)自定義錯誤信息返回詳解
這篇文章主要為大家詳細(xì)介紹了如何基于Spring實(shí)現(xiàn)自定義錯誤信息返回效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-03-03
java如何更改數(shù)據(jù)庫中的數(shù)據(jù)
這篇文章主要介紹了java如何更改數(shù)據(jù)庫中的數(shù)據(jù),修改數(shù)據(jù)庫是數(shù)據(jù)庫操作必不可少的一部分,使用Statement接口中的excuteUpdate()方法可以修改數(shù)據(jù)表中的數(shù)據(jù),感興趣的朋友跟隨小編一起看看吧2021-11-11
javaweb Servlet開發(fā)總結(jié)(一)
Servlet是sun公司提供的一門用于開發(fā)動態(tài)web資源的技術(shù)。這篇文章主要介紹了javaweb Servlet開發(fā)的第一篇,感興趣的小伙伴們可以參考一下2016-05-05
Java socket通訊實(shí)現(xiàn)過程及問題解決
這篇文章主要介紹了Java socket通訊實(shí)現(xiàn)過程及問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01
Java讀取網(wǎng)絡(luò)文件的實(shí)例代碼
這篇文章主要介紹了Java讀取網(wǎng)絡(luò)文件的實(shí)例代碼,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07

