Java將集合元素轉(zhuǎn)換為數(shù)組的三種方式
在實際開發(fā)中,雖然用集合(Collection)來存儲元素通常更靈活,但有些場景下,我們仍然需要將集合轉(zhuǎn)換成數(shù)組(比如與老舊 API 交互、需要固定大小存儲等)。
Java 的 Collection 接口提供了 三種方式 來實現(xiàn)這一點,都是通過 toArray() 方法的不同重載實現(xiàn)的。
?? 三種toArray()使用方式
| 方法 | 返回 | 特點 |
|---|---|---|
| toArray() | Object[] | 返回一個對象數(shù)組,不帶類型信息 |
| toArray(T[] array) | T[] | 返回一個指定類型的數(shù)組,需要傳入一個數(shù)組 |
| toArray(IntFunction<T[]> generator) | T[] | 使用構(gòu)造器引用創(chuàng)建數(shù)組,代碼更簡潔(JDK 8+) |
1. ??? 使用無參toArray()
這種方式最簡單:直接調(diào)用 toArray(),返回的是一個 Object[] 類型的數(shù)組。
Collection<String> strings = List.of("one", "two", "three");
Object[] array = strings.toArray();
System.out.println(Arrays.toString(array));
??? 輸出結(jié)果:
[one, two, three]
?? 注意: 這種方式得到的是 Object[],如果你強制轉(zhuǎn)換成 String[],可能會在運行時拋出異常(ClassCastException)。 因此,一般不推薦在需要明確類型時使用這種方式。
2. ??? 使用帶數(shù)組參數(shù)的toArray(T[] array)
這種方式可以直接得到正確類型的數(shù)組,比如 String[],而且避免類型轉(zhuǎn)換出錯。
Collection<String> strings = List.of("one", "two");
// 方式1:傳入一個長度為0的數(shù)組
String[] result1 = strings.toArray(new String[0]);
System.out.println(Arrays.toString(result1));
// 方式2:傳入一個足夠大的數(shù)組
String[] largerArray = new String[5];
String[] result2 = strings.toArray(largerArray);
System.out.println(Arrays.toString(result2));
??? 輸出結(jié)果:
[one, two]
[one, two, null, null, null]
? 背后細節(jié)解釋
- 如果傳入的數(shù)組長度大于等于集合的大小,元素將被直接拷貝到這個數(shù)組中;
- 如果傳入的數(shù)組長度小于集合的大小,Java會創(chuàng)建一個新數(shù)組,大小正好適配集合元素;
- 如果數(shù)組比集合大,拷貝完元素后,第一個空余位置會設(shè)為
null,剩下的位置保持原值。
?? 示例:傳入比集合大的數(shù)組
Collection<String> strings = List.of("one", "two");
String[] largerTab = {"old1", "old2", "old3", "old4"};
System.out.println("Before: " + Arrays.toString(largerTab));
String[] result = strings.toArray(largerTab);
System.out.println("After : " + Arrays.toString(result));
System.out.println("Same array? " + (result == largerTab));
??? 輸出結(jié)果:
Before: [old1, old2, old3, old4]
After : [one, two, null, old4]
Same array? true
? 注意:返回的是傳入的原數(shù)組,而不是新建的數(shù)組!
3. ??? 使用toArray(IntFunction<T[]> generator)(推薦)
從 Java 11 開始,你可以使用構(gòu)造器引用(例如 String[]::new)來創(chuàng)建數(shù)組,更加優(yōu)雅、簡潔!
Collection<String> strings = List.of("one", "two", "three");
String[] array = strings.toArray(String[]::new);
System.out.println(Arrays.toString(array));
??? 輸出結(jié)果:
[one, two, three]
?? 小結(jié)
| 方法 | 優(yōu)點 | 缺點 |
|---|---|---|
| toArray() | 簡單直接 | 只返回 Object[],需要小心類型 |
| toArray(T[] array) | 返回指定類型,兼容老版本Java | 代碼稍長,需要處理數(shù)組長度 |
| toArray(IntFunction<T[]> generator) | 代碼最簡潔,推薦(Java 11+) | 需要理解構(gòu)造器引用語法 |
到此這篇關(guān)于Java將集合元素轉(zhuǎn)換為數(shù)組的三種方式的文章就介紹到這了,更多相關(guān)Java 集合元素轉(zhuǎn)換為數(shù)組內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java多線程程序中synchronized修飾方法的使用實例
synchronized關(guān)鍵字主要北用來進行線程同步,這里我們主要來演示Java多線程程序中synchronized修飾方法的使用實例,需要的朋友可以參考下:2016-06-06
淺談springboot之JoinPoint的getSignature方法
這篇文章主要介紹了springboot之JoinPoint的getSignature方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
springboot發(fā)布dubbo服務(wù)注冊到nacos實現(xiàn)方式
這篇文章主要介紹了springboot發(fā)布dubbo服務(wù)注冊到nacos實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09

