How do I clone a generic List in Java?

前端 未结 14 1969
轻奢々
轻奢々 2020-11-27 12:38

I have an ArrayList that I\'d like to return a copy of. ArrayList has a clone method which has the following signature:



        
14条回答
  •  清酒与你
    2020-11-27 13:12

    With Java 8 it can be cloned with a stream.

    import static java.util.stream.Collectors.toList;
    

    ...

    List clone = myList.stream().collect(toList());
    

提交回复
热议问题