java逗號分隔String字符串及數(shù)組、集合相互轉(zhuǎn)換
1. 準(zhǔn)備一個逗號分割字符串
String str = "小張,小王,小李,小趙";
2. 逗號分割字符串轉(zhuǎn)換為集合(轉(zhuǎn)換為集合之前會先轉(zhuǎn)換為數(shù)組)
// 第一種:先用split將字符串按逗號分割為數(shù)組,再用Arrays.asList將數(shù)組轉(zhuǎn)換為集合
List<String> strList1 = Arrays.asList(str.split(","));
// 第二種:使用stream轉(zhuǎn)換String集合
List<String> strList2 = Arrays.stream(str.split(",")).collect(Collectors.toList());
// 第三種:使用stream轉(zhuǎn)換int集合(這種適用字符串是逗號分隔的類型為int類型)
List<Integer> intList = Arrays.stream(str.split(",")).map(Integer::parseInt).collect(Collectors.toList());
// 第四種:使用Guava的SplitterString
List<String> strList3= Splitter.on(",").trimResults().splitToList(str);
// 第五種:使用Apache Commons的StringUtils(只用到了他的split)
List<String> strList4= Arrays.asList(StringUtils.split(str,","));
// 第六種:使用Spring Framework的StringUtils
List<String> strList5 =Arrays.asList(StringUtils.commaDelimitedListToStringArray(str));
3. 集合轉(zhuǎn)換為逗號分隔的字符串
// 第一種:String.join(), JDK1.8+
str = String.join(",", strList1);
// 第二種:org.apache.commons.lang3.StringUtils. toArray():集合轉(zhuǎn)換為數(shù)組
str = StringUtils.join(strList1.toArray(), ",");
// 第三種:需要引入hutool工具包
str = Joiner.on(",").join(strList1);
// 第四種:StringJoiner, JDK1.8+ 輸出示例:START_小張,小王,小李,小趙_END
StringJoiner sj = new StringJoiner(",");
strList1.forEach(e -> sj.add(String.valueOf(e)));
// 在上面已經(jīng)處理為逗號拼接的字符串,下面為補(bǔ)充
// 在連接之前操作字符串, 拼接前綴和后綴
StringJoiner sj2 = new StringJoiner(",", "START_", "_END");
strList1.forEach(e -> sj2.add(String.valueOf(e)));
// 第五種:Stream, Collectors.joining(), JDK1.8+
str = strList1.stream().collect(Collectors.joining(","));
// 在連接之前操作字符串, 拼接前綴和后綴. 輸出示例:START_小張,小王,小李,小趙_END
str = strList1.stream().map(e -> {
if (e != null) return e.toUpperCase();
else return "null";
}).collect(Collectors.joining(",", "START_", "_END"));
// 第六種:使用Spring Framework的StringUtils
str = StringUtils.collectionToDelimitedString(strList1,",");
4. 數(shù)組轉(zhuǎn)逗號分隔字符串
String [] arr = (String[])strList1.toArray();
// 第一種:使用StringUtils的join方法
str = StringUtils.join(arr, ",");
// 第二種:使用ArrayUtils的toString方法,這種方式轉(zhuǎn)換的字符串首尾加大括號 輸出示例:{小張,小王,小李,小趙}
ArrayUtils.toString(arr, ",");補(bǔ)充:將一個Java字符串按逗號分割成一個列表(List)
可以使用Java的split()方法將字符串分割成字符串?dāng)?shù)組,然后將數(shù)組轉(zhuǎn)換為列表。以下是一個示例代碼:
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
String str = "apple,banana,orange";
List<String> list = Arrays.asList(str.split(","));
System.out.println(list);
}
}
輸出
[apple, banana, orange]
在上述代碼中,我們使用split(",")方法將字符串str按逗號分割成一個字符串?dāng)?shù)組,然后使用Arrays.asList()方法將數(shù)組轉(zhuǎn)換為列表。最后,我們打印輸出列表的內(nèi)容。
請注意,Arrays.asList()方法返回的是一個固定大小的列表,不支持對其進(jìn)行添加或刪除操作。如果需要進(jìn)行修改操作,可以將其轉(zhuǎn)換為ArrayList,如下所示:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
String str = "apple,banana,orange";
List<String> list = new ArrayList<>(Arrays.asList(str.split(",")));
System.out.println(list);
}
}總結(jié)
到此這篇關(guān)于java逗號分隔String字符串及數(shù)組、集合相互轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)java逗號分隔String字符串內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot 自動配置的實(shí)現(xiàn)
這篇文章主要介紹了Spring Boot 自動配置的實(shí)現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
SVN出現(xiàn)提示org.apache.subversion.javahl.ClientException: Attempt
這篇文章主要介紹了SVN出現(xiàn)提示org.apache.subversion.javahl.ClientException: Attempted to lock an already-locked dir解決方案的相關(guān)資料,需要的朋友可以參考下2016-12-12
Java對象以Hash結(jié)構(gòu)存入Redis詳解
這篇文章主要介紹了Java對象以Hash結(jié)構(gòu)存入Redis詳解,和Java中的對象非常相似,卻不能按照J(rèn)ava對象的結(jié)構(gòu)直接存儲進(jìn)Redis的hash中,因?yàn)镴ava對象中的field是可以嵌套的,而Redis的Hash結(jié)構(gòu)不支持嵌套結(jié)構(gòu),需要的朋友可以參考下2023-08-08
一文詳解如何配置MyBatis實(shí)現(xiàn)打印可執(zhí)行的SQL語句
在MyBatis中,動態(tài)SQL是一個強(qiáng)大的特性,允許我們在XML映射文件或注解中編寫條件語句,根據(jù)運(yùn)行時(shí)的參數(shù)來決定SQL的具體執(zhí)行內(nèi)容,這篇文章主要給大家介紹了關(guān)于如何配置MyBatis實(shí)現(xiàn)打印可執(zhí)行的SQL語句的相關(guān)資料,需要的朋友可以參考下2024-08-08
Java設(shè)計(jì)模式之中介模式(Mediator模式)介紹
這篇文章主要介紹了Java設(shè)計(jì)模式之中介模式(Mediator模式)介紹,本文講解了為何使用Mediator模式、如何使用中介模式等內(nèi)容,需要的朋友可以參考下2015-03-03

