Java中ArrayList的8種淺拷貝方式示例代碼
引言
嗨,小伙伴們!今天咱們來聊聊 Java 中的 ArrayList 淺拷貝。啥是淺拷貝呢?簡單來說,就是復制對象時只復制指向對象的引用,而不復制對象本身。那么,如何在 Java 中對 ArrayList 進行淺拷貝呢?別急,咱們這就開始揭秘!
什么是淺拷貝?
淺拷貝是指復制對象時只復制對象的引用地址,而不復制對象本身。因此,原始對象和拷貝對象共享同一份數(shù)據(jù)。在 Java 中,當我們對 ArrayList 進行淺拷貝時,我們只是復制了 ArrayList 中元素的引用,而不是元素本身。
ArrayList 淺拷貝的重要性
在日常開發(fā)中,我們經(jīng)常需要對 ArrayList 進行操作而不影響原集合。這時候,淺拷貝就能派上大用場了。下面我們來一起看看八種實現(xiàn)淺拷貝的方法吧!
方法一:使用構造函數(shù)
使用 ArrayList 的構造函數(shù)創(chuàng)建一個新的 ArrayList,并傳入原 ArrayList 作為參數(shù)。
List<String> originalList = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); List<String> shallowCopy = new ArrayList<>(originalList);
方法二:使用 addAll() 方法
通過將原 ArrayList 的所有元素添加到新 ArrayList 中來實現(xiàn)淺拷貝。
List<String> originalList = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); List<String> shallowCopy = new ArrayList<>(); shallowCopy.addAll(originalList);
方法三:使用 System.arraycopy()
雖然這個方法通常用于數(shù)組拷貝,但我們可以先將 ArrayList 轉換為數(shù)組,再進行拷貝,最后再轉換回 ArrayList。
List<String> originalList = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); String[] array = originalList.toArray(new String[0]); String[] shallowCopyArray = new String[array.length]; System.arraycopy(array, 0, shallowCopyArray, 0, array.length); List<String> shallowCopy = new ArrayList<>(Arrays.asList(shallowCopyArray));
方法四:使用 Collections.copy()
這個方法非常方便,直接使用 Collections.copy()
方法即可完成淺拷貝。
List<String> originalList = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); List<String> shallowCopy = new ArrayList<>(originalList.size()); Collections.copy(shallowCopy, originalList);
方法五:使用 Java 8 Stream API
從 Java 8 開始,Stream API 提供了更簡潔的方式來實現(xiàn)淺拷貝。
List<String> originalList = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); List<String> shallowCopy = originalList.stream().collect(Collectors.toList());
方法六:使用 clone() 方法
如果 ArrayList 實現(xiàn)了 Cloneable
接口,我們可以直接使用 clone()
方法。
List<String> originalList = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); List<String> shallowCopy = null; try { shallowCopy = (List<String>) originalList.clone(); } catch (CloneNotSupportedException e) { // Handle exception }
注意:ArrayList
類默認實現(xiàn)了 Cloneable
接口,所以 clone()
方法可以直接使用。
方法七:使用 Arrays.asList() 方法
如果原 ArrayList 包含的是基本類型或者不可變對象,我們可以直接使用 Arrays.asList()
方法。
List<Integer> originalList = Arrays.asList(1, 2, 3); List<Integer> shallowCopy = new ArrayList<>(Arrays.asList(originalList.toArray()));
方法八:使用 Guava 庫
Guava 庫提供了實用的集合工具類 Lists
,可以輕松實現(xiàn)淺拷貝。
List<String> originalList = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); List<String> shallowCopy = Lists.newArrayList(originalList);
演練步驟
讓我們通過一個具體的例子來詳細了解這些淺拷貝的方法。
示例代碼
假設我們有一個原始的 ArrayList originalList
,我們將使用上述方法對其進行淺拷貝。
import java.util.*; public class ArrayListShallowCopyDemo { public static void main(String[] args) { List<String> originalList = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); // 方法一:使用構造函數(shù) List<String> shallowCopy1 = new ArrayList<>(originalList); // 方法二:使用 addAll() List<String> shallowCopy2 = new ArrayList<>(); shallowCopy2.addAll(originalList); // 方法三:使用 System.arraycopy() String[] array = originalList.toArray(new String[0]); String[] shallowCopyArray = new String[array.length]; System.arraycopy(array, 0, shallowCopyArray, 0, array.length); List<String> shallowCopy3 = new ArrayList<>(Arrays.asList(shallowCopyArray)); // 方法四:使用 Collections.copy() List<String> shallowCopy4 = new ArrayList<>(originalList.size()); Collections.copy(shallowCopy4, originalList); // 方法五:使用 Java 8 Stream API List<String> shallowCopy5 = originalList.stream().collect(Collectors.toList()); // 方法六:使用 clone() List<String> shallowCopy6 = null; try { shallowCopy6 = (List<String>) originalList.clone(); } catch (CloneNotSupportedException e) { // Handle exception } // 方法七:使用 Arrays.asList() (適用于基本類型或不可變對象) List<Integer> originalIntList = Arrays.asList(1, 2, 3); List<Integer> shallowCopy7 = new ArrayList<>(Arrays.asList(originalIntList.toArray())); // 方法八:使用 Guava 庫 List<String> shallowCopy8 = com.google.common.collect.Lists.newArrayList(originalList); // 輸出拷貝后的列表 System.out.println("Original List: " + originalList); System.out.println("Shallow Copy 1: " + shallowCopy1); System.out.println("Shallow Copy 2: " + shallowCopy2); System.out.println("Shallow Copy 3: " + shallowCopy3); System.out.println("Shallow Copy 4: " + shallowCopy4); System.out.println("Shallow Copy 5: " + shallowCopy5); System.out.println("Shallow Copy 6: " + shallowCopy6); System.out.println("Shallow Copy 7: " + shallowCopy7); System.out.println("Shallow Copy 8: " + shallowCopy8); } }
運行結果
當我們運行上面的程序后,可以看到所有拷貝后的列表都包含了原始列表的數(shù)據(jù)。
性能比較
每種淺拷貝方法都有其適用場景。例如,使用構造函數(shù)或 addAll()
方法是最簡單且易于理解的;而使用 System.arraycopy()
或 Collections.copy()
在某些情況下可能更高效;使用 Java 8 的 Stream API 則提供了更簡潔的語法。
實際案例分析
讓我們通過一個實際的性能測試來看看不同淺拷貝方法的效果。
示例代碼
假設我們有一個包含大量數(shù)據(jù)的 ArrayList,我們將比較不同淺拷貝方法的性能差異。
總結
通過上述例子,我們可以總結出:
- 不同的淺拷貝方法各有優(yōu)缺點,選擇合適的方法取決于具體的應用場景。
- 使用構造函數(shù)或
addAll()
方法是最簡單直接的方式。 - 使用 Java 8 Stream API 或 Guava 庫可以讓代碼看起來更加現(xiàn)代。
互動提問
你在項目中是如何實現(xiàn) ArrayList 的淺拷貝的?你更傾向于使用哪種方法呢?歡迎在評論區(qū)分享你的經(jīng)驗!
附錄
在實際使用中,還需要考慮以下幾點:
- 性能考量:對于大型列表,某些方法可能更高效。
- 內存消耗:淺拷貝不會增加額外的對象內存消耗。
- 對象類型:對于可變對象,淺拷貝可能會導致意外的副作用。
附錄
為了進一步理解淺拷貝的過程,我們可以利用一些工具來輔助分析,例如使用 Java Profiler 來監(jiān)控內存使用情況,以及使用 JUnit 測試框架來進行性能測試。
結語
希望這篇文章能幫助你更好地理解和運用 Java 中 ArrayList 的淺拷貝方法。
到此這篇關于Java中ArrayList的8種淺拷貝方式的文章就介紹到這了,更多相關Java中ArrayList淺拷貝方式內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Jenkins集成sonarQube實現(xiàn)代碼質量檢查過程圖解
這篇文章主要介紹了Jenkins集成sonarQube實現(xiàn)代碼質量檢查過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09SpringBoot+Resilience4j實現(xiàn)接口限流的示例代碼
Resilience4j 是一個用于實現(xiàn)熔斷、限流、重試等功能的輕量級庫,本文主要介紹了SpringBoot+Resilience4j實現(xiàn)接口限流的示例代碼,具有一定的參考價值,感興趣的可以了解一下2024-12-12Java重寫(Override)與重載(Overload)區(qū)別原理解析
這篇文章主要介紹了Java重寫(Override)與重載(Overload)區(qū)別原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-02-02