Copy ArrayList的四种方式
生活随笔
收集整理的這篇文章主要介紹了
Copy ArrayList的四种方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 簡介
- 使用構造函數
- 使用addAll方法
- 使用Collections.copy
- 使用stream
- 總結
Copy ArrayList的四種方式
簡介
ArrayList是我們經常會用到的集合類,有時候我們需要拷貝一個ArrayList,今天向大家介紹拷貝ArrayList常用的四種方式。
使用構造函數
ArrayList有個構造函數,可以傳入一個集合:
public ArrayList(Collection<? extends E> c) {elementData = c.toArray();if ((size = elementData.length) != 0) {// c.toArray might (incorrectly) not return Object[] (see 6260652)if (elementData.getClass() != Object[].class)elementData = Arrays.copyOf(elementData, size, Object[].class);} else {// replace with empty array.this.elementData = EMPTY_ELEMENTDATA;}}上面的代碼我們可以看出,底層實際上調用了Arrays.copyOf方法來對數組進行拷貝。這個拷貝調用了系統的native arraycopy方法,注意這里的拷貝是引用拷貝,而不是值的拷貝。這就意味著這如果拷貝之后對象的值發送了變化,源對象也會發生改變。
舉個例子:
@Testpublic void withConstructor(){List<String> stringList=new ArrayList<>(Arrays.asList("a","b","c"));List<String> copyList = new ArrayList<>(stringList);copyList.set(0,"e");log.info("{}",stringList);log.info("{}",copyList);List<CustBook> objectList=new ArrayList<>(Arrays.asList(new CustBook("a"),new CustBook("b"),new CustBook("c")));List<CustBook> copyobjectList = new ArrayList<>(objectList);copyobjectList.get(0).setName("e");log.info("{}",objectList);log.info("{}",copyobjectList);}運行結果:
22:58:39.001 [main] INFO com.flydean.CopyList - [a, b, c] 22:58:39.008 [main] INFO com.flydean.CopyList - [e, b, c] 22:58:39.009 [main] INFO com.flydean.CopyList - [CustBook(name=e), CustBook(name=b), CustBook(name=c)] 22:58:39.009 [main] INFO com.flydean.CopyList - [CustBook(name=e), CustBook(name=b), CustBook(name=c)]我們看到對象的改變實際上改變了拷貝的源。而copyList.set(0,“e”)實際上創建了一個新的String對象,并把它賦值到copyList的0位置。
使用addAll方法
List有一個addAll方法,我們可以使用這個方法來進行拷貝:
@Testpublic void withAddAll(){List<CustBook> objectList=new ArrayList<>(Arrays.asList(new CustBook("a"),new CustBook("b"),new CustBook("c")));List<CustBook> copyobjectList = new ArrayList<>();copyobjectList.addAll(objectList);copyobjectList.get(0).setName("e");log.info("{}",objectList);log.info("{}",copyobjectList);}同樣的拷貝的是對象的引用。
使用Collections.copy
同樣的,使用Collections.copy也可以得到相同的效果,看下代碼:
@Testpublic void withCopy(){List<CustBook> objectList=new ArrayList<>(Arrays.asList(new CustBook("a"),new CustBook("b"),new CustBook("c")));List<CustBook> copyobjectList = new ArrayList<>(Arrays.asList(new CustBook("d"),new CustBook("e"),new CustBook("f")));Collections.copy(copyobjectList, objectList);copyobjectList.get(0).setName("e");log.info("{}",objectList);log.info("{}",copyobjectList);}使用stream
我們也可以使用java 8引入的stream來實現:
@Testpublic void withStream(){List<CustBook> objectList=new ArrayList<>(Arrays.asList(new CustBook("a"),new CustBook("b"),new CustBook("c")));List<CustBook> copyobjectList=objectList.stream().collect(Collectors.toList());copyobjectList.get(0).setName("e");log.info("{}",objectList);log.info("{}",copyobjectList);}總結
好了,四種方法講完了,大家要注意四種方法都是引用拷貝,在使用的時候要小心。
本文的例子https://github.com/ddean2009/learn-java-collections
更多精彩內容且看:
- 區塊鏈從入門到放棄系列教程-涵蓋密碼學,超級賬本,以太坊,Libra,比特幣等持續更新
- Spring Boot 2.X系列教程:七天從無到有掌握Spring Boot-持續更新
- Spring 5.X系列教程:滿足你對Spring5的一切想象-持續更新
- java程序員從小工到專家成神之路(2020版)-持續更新中,附詳細文章教程
歡迎關注我的公眾號:程序那些事,更多精彩等著您!
更多內容請訪問 www.flydean.com
總結
以上是生活随笔為你收集整理的Copy ArrayList的四种方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Iterator to list的三种方
- 下一篇: 深入理解HashMap和TreeMap的