Java實(shí)現(xiàn)字符串轉(zhuǎn)List的常見(jiàn)方式總結(jié)
在Java開(kāi)發(fā)中,我們經(jīng)常需要將字符串轉(zhuǎn)換為L(zhǎng)ist類型,以便進(jìn)行后續(xù)處理。下面介紹幾種常見(jiàn)的字符串轉(zhuǎn)List的方式,并給出相應(yīng)的使用教程。
一、使用Arrays.asList()方法
如果你有一個(gè)由逗號(hào)分隔的字符串,并希望將其拆分為一個(gè)List,你可以使用Arrays.asList()配合String.split()方法來(lái)實(shí)現(xiàn)。
import java.util.Arrays;
import java.util.List;
public class StringToListExample {
public static void main(String[] args) {
String str = "item1,item2,item3";
List<String> list = Arrays.asList(str.split(","));
System.out.println(list); // 輸出: [item1, item2, item3]
}
}
注意:Arrays.asList()返回的List是固定大小的,不支持添加或刪除元素。如果需要可變的List,可以將其轉(zhuǎn)換為ArrayList。
List<String> mutableList = new ArrayList<>(Arrays.asList(str.split(",")));
二、使用Stream API
Java 8引入了Stream API,可以更方便地對(duì)集合進(jìn)行操作。你可以使用Arrays.stream()或Pattern.compile()與String.splitAsStream()來(lái)將字符串轉(zhuǎn)換為流,然后收集為L(zhǎng)ist。
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StringToListWithStreamExample {
public static void main(String[] args) {
String str = "item1,item2,item3";
// 使用Arrays.stream()
List<String> list1 = Stream.of(str.split(","))
.collect(Collectors.toList());
System.out.println(list1); // 輸出: [item1, item2, item3]
// 使用Pattern.compile()與splitAsStream()
List<String> list2 = Pattern.compile(",")
.splitAsStream(str)
.collect(Collectors.toList());
System.out.println(list2); // 輸出: [item1, item2, item3]
}
}
三、使用Guava庫(kù)
Guava是Google提供的一個(gè)Java核心庫(kù),它提供了很多實(shí)用的功能,包括字符串到List的轉(zhuǎn)換。
首先,你需要在項(xiàng)目中添加Guava的依賴:
<!-- Maven依賴 -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>最新版本號(hào)</version>
</dependency>然后,你可以使用Guava的Splitter類來(lái)分割字符串并轉(zhuǎn)換為L(zhǎng)ist。
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
public class StringToListWithGuavaExample {
public static void main(String[] args) {
String str = "item1,item2,item3";
Iterable<String> iterable = Splitter.on(',').trimResults().split(str);
List<String> list = Lists.newArrayList(iterable);
System.out.println(list); // 輸出: [item1, item2, item3]
}
}
四、使用Apache Commons Lang庫(kù)
Apache Commons Lang庫(kù)也提供了字符串操作的功能,包括將字符串轉(zhuǎn)換為L(zhǎng)ist。
首先,你需要在項(xiàng)目中添加Apache Commons Lang的依賴:
<!-- Maven依賴 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>最新版本號(hào)</version>
</dependency>然后,你可以使用StringUtils類的split()方法結(jié)合Arrays.asList()或轉(zhuǎn)換為ArrayList。
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class StringToListWithCommonsLangExample {
public static void main(String[] args) {
String str = "item1,item2,item3";
String[] array = StringUtils.split(str, ',');
List<String> list = Arrays.asList(array); // 固定大小的List
// 或者
List<String> mutableList = new ArrayList<>(Arrays.asList(array)); // 可變的List
System.out.
五、方法補(bǔ)充
以下代碼演示了使用阿里巴巴 Fastjson 庫(kù)將 JSON 字符串轉(zhuǎn)換為 Java 對(duì)象列表的兩種常用方式。
方式一:通過(guò) JSONObject.parseObject 結(jié)合 TypeReference 實(shí)現(xiàn)泛型反序列化。首先創(chuàng)建空的 ArrayList 對(duì)象,然后調(diào)用 parseObject 方法,傳入 JSON 字符串和 TypeReference<List> 類型引用,即可將 JSON 數(shù)組轉(zhuǎn)換為指定泛型類型的對(duì)象列表。這種方式適用于需要明確指定泛型類型的場(chǎng)景,能夠避免類型擦除問(wèn)題。
方式二:通過(guò) JSONArray.parseArray 直接進(jìn)行轉(zhuǎn)換。該方法更加簡(jiǎn)潔,直接傳入 JSON 字符串和目標(biāo)類的 Class 對(duì)象,即可快速將 JSON 數(shù)組轉(zhuǎn)換為對(duì)應(yīng)類型的列表。這種方式適用于結(jié)構(gòu)簡(jiǎn)單、類型明確的場(chǎng)景。
兩種方式均可實(shí)現(xiàn) JSON 到 Java 列表的轉(zhuǎn)換,方式一通過(guò) TypeReference 保留了泛型信息,方式二則更加簡(jiǎn)潔直觀,開(kāi)發(fā)者可根據(jù)實(shí)際需求和編碼習(xí)慣選擇使用。
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.JSONObject;
String str = "[
{
"id": 5,
"nodeIdArr": "[\"221\",\"222\"]",
"nodeNameArr": "[\"enb_221\",\"2222\"]",
"upperLimitOfTheBusyTimeThreshold": 9,
"lowerLimitOfTheBusyTimeThreshold": 2,
"dateRangeBeginTime": 1701648000000,
"dateRangeEndTime": 1701682200000,
"createTime": 1701676594000,
"updateTime": 1701737385000,
"activeState": "1"
},
{
"id": 6,
"nodeIdArr": "[\"2003\",\"501\",\"10010\"]",
"nodeNameArr": "[\"CityA\",\"501\",\"Vir1\"]",
"upperLimitOfTheBusyTimeThreshold": 9,
"lowerLimitOfTheBusyTimeThreshold": 2,
"dateRangeBeginTime": 1701648000000,
"dateRangeEndTime": 1701682200000,
"createTime": 1701676641000,
"updateTime": 1701737382000,
"activeState": "1"
}]"
List<BusyTimeIndicatorAlarmThreshold> busyTimeIndicatorAlarmThresholdList = new ArrayList<>();
busyTimeIndicatorAlarmThresholdList = JSONObject.parseObject(str, new TypeReference<List<BusyTimeIndicatorAlarmThreshold>>() {});方式一
List busyTimeIndicatorAlarmThresholdList = new ArrayList<>();
busyTimeIndicatorAlarmThresholdList = JSONObject.parseObject(str, new TypeReference<List>() {});方式二
List userList = JSONArray.parseArray(str, User.class);
到此這篇關(guān)于Java實(shí)現(xiàn)字符串轉(zhuǎn)List的常見(jiàn)方式總結(jié)的文章就介紹到這了,更多相關(guān)Java字符串轉(zhuǎn)List內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot微服務(wù)打包Docker鏡像流程解析
這篇文章主要介紹了Springboot微服務(wù)打包Docker鏡像流程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
IntelliJ IDEA 常用設(shè)置(配置)吐血整理(首次安裝必需)
這篇文章主要介紹了IntelliJ IDEA 常用設(shè)置(配置)吐血整理(首次安裝必需),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
SpringBoot中處理跨域資源的5種實(shí)現(xiàn)方法與對(duì)比
這篇文章主要為大家詳細(xì)介紹了SpringBoot中處理跨域資源的5種實(shí)現(xiàn)方法并進(jìn)行簡(jiǎn)單對(duì)比,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2026-01-01
SpringBoot如何優(yōu)雅的處理校驗(yàn)參數(shù)的方法
這篇文章主要介紹了SpringBoot如何優(yōu)雅的處理校驗(yàn)參數(shù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
關(guān)于spring中bean注冊(cè)的優(yōu)先級(jí)分析
Spring框架中,Bean的定義方式主要有三種:XML定義、注解掃描和配置類中的@Bean注解,在Bean注冊(cè)過(guò)程中,XML定義的GenericBeanDefinition優(yōu)先級(jí)最高2024-09-09
SpringSecurity rememberme功能實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了SpringSecurity rememberme功能實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
SpringLDAP目錄服務(wù)之LdapTemplate與LDAP操作方式
本文將深入探討Spring LDAP的核心概念、LdapTemplate的使用方法以及如何執(zhí)行常見(jiàn)的LDAP操作,幫助開(kāi)發(fā)者有效地將LDAP集成到Spring應(yīng)用中,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04

