Java?在?Array?和?Set?之間進行轉(zhuǎn)換的示例
概述
在本文章中,我們對如何在 Java 中對 Array 和 Set 進行轉(zhuǎn)換進行一些說明和示例。
這些示例通過使用 Core Java 和一些第三方的轉(zhuǎn)換工具,例如 Guava 和 Apache Commons Collections。
從 List 轉(zhuǎn)換為 Set
使用原生 Java 代碼
讓我們首先來看看如何在原生 Java 中把數(shù)組轉(zhuǎn)換為 Set。
通過下面的代碼,我們了解到首先需要把 Array 轉(zhuǎn)換為 List,然后再把這個 List 轉(zhuǎn)換為 Set。
@Test
public void givenUsingCoreJavaV1_whenArrayConvertedToSet_thenCorrect() {
Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 };
Set<Integer> targetSet = new HashSet<Integer>(Arrays.asList(sourceArray));
}可選的,我們可以首先定義一個 Set 對象,然后把這個 Set 對象的元素進行填充:
@Test
public void givenUsingCoreJavaV2_whenArrayConvertedToSet_thenCorrect() {
Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 };
Set<Integer> targetSet = new HashSet<Integer>();
Collections.addAll(targetSet, sourceArray);
}使用 Guava
我們可以使用 Guava 轉(zhuǎn)換工具來把數(shù)組給出的數(shù)組來進行初始化。
@Test
public void givenUsingGuava_whenArrayConvertedToSet_thenCorrect() {
Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 };
Set<Integer> targetSet = Sets.newHashSet(sourceArray);
}使用 Apache Commons Collections
最后我們可以使用 Apache 的 Commons Collection 的庫來進行處理。
這個處理方法和我們使用原生 Java 代碼差不多,首先需要對 Set 進行初始化,然后再把 Set 中的元素進行填充。
@Test
public void givenUsingCommonsCollections_whenArrayConvertedToSet_thenCorrect() {
Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 };
Set<Integer> targetSet = new HashSet<>(6);
CollectionUtils.addAll(targetSet, sourceArray);
}從 Set 轉(zhuǎn)換為 Array
使用原生 Java
Set 中有一個 toArray 的方法,你可以直接使用這個方法來把給出的 Set 轉(zhuǎn)換為 Array。
@Test
public void givenUsingCoreJava_whenSetConvertedToArray_thenCorrect() {
Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
Integer[] targetArray = sourceSet.toArray(new Integer[0]);
}需要注意的是,我們在這里使用了 toArray(new T[0]) 來對變量進行初始化,相對使用 toArray(new T[size]) 這個方法。
使用 toArray(new T[0]) 來對數(shù)組進行初始化更加安全,快速,易讀。
使用 Guava
下一步,讓我們來使用 Guava 的 API 來進行轉(zhuǎn)換。
@Test
public void givenUsingGuava_whenSetConvertedToArray_thenCorrect() {
Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
int[] targetArray = Ints.toArray(sourceSet);
}Ints 中有一個 toArray 的方法,這個方法將會把整數(shù)類型的 List 轉(zhuǎn)換為 Array。
需要注意的是 Ints 的類型需要和數(shù)組中的類型進行匹配才可以。
結(jié)論
在 Java 的集合類型直接進行轉(zhuǎn)換是我們需要經(jīng)常進行操作的方法和需求。
Guava 的方法 Sets 中通常能夠?qū)?Set 進行比較好的操作,包括對 Set 的對象進行初始化。
我們可以用這個對象中的初始化參數(shù)來對數(shù)組轉(zhuǎn)換為 Set。
使用 Commons Collections 還是有點晦澀。
到此這篇關(guān)于Java 如何在 Array 和 Set 之間進行轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)Java Array 和 Set 轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Maven install 報錯"程序包不存在"問題的解決方法
這篇文章主要介紹了Maven install 報錯"程序包不存在"問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Java?ThreadPoolExecutor線程池有關(guān)介紹
這篇文章主要介紹了Java?ThreadPoolExecutor線程池有關(guān)介紹,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09
金三銀四復(fù)工高頻面試題java算法LeetCode396旋轉(zhuǎn)函數(shù)
這篇文章主要為大家介紹了金三銀四復(fù)工高頻面試題之java算法題解LeetCode396旋轉(zhuǎn)函數(shù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
解決使用json-lib包實現(xiàn)xml轉(zhuǎn)json時空值被轉(zhuǎn)為空中括號的問題
網(wǎng)上能查到的xml轉(zhuǎn)json的jar包大部分是net.sf.json-lib,但是JSON json =xmlSerializer.read(xml); 方法會出現(xiàn)將空值轉(zhuǎn)化為[]的問題,下面為大家提供兩種解決方法2018-03-03
elasticsearch索引index數(shù)據(jù)功能源碼示例
這篇文章主要為大家介紹了elasticsearch索引index功能源碼示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04

