Copying sets Java

后端 未结 5 948
清歌不尽
清歌不尽 2021-01-31 01:19

Is there a way to copy a TreeSet? That is, is it possible to go

Set  itemList;
Set  tempList;

tempList = itemList;
<         


        
5条回答
  •  迷失自我
    2021-01-31 01:38

    Starting from Java 10:

    Set oldSet = Set.of();
    Set newSet = Set.copyOf(oldSet);
    

    Set.copyOf() returns an unmodifiable Set containing the elements of the given Collection.

    The given Collection must not be null, and it must not contain any null elements.

提交回复
热议问题