Arrays.asList()vs Collections.singletonList()

Collections.singletonList(something)不可变的,

Arrays.asList(something)是List和Array在堆中加入的Array的一个固定大小的List表示形式。

Arrays.asList(something)允许Arrays.asList(something) 更改 ,这反映到List和联合数组中。

它会抛出UnsupportedOperationException用于添加,删除元素,尽管您可以为特定索引设置元素。

Collections.singletonList(something)返回的列表所做的任何更改将导致UnsupportedOperationException 。

此外,由Collections.singletonList(something)返回的List的容量将始终为1,

        而Arrays.asList(something)的容量将为已支持数组的大小。

 

猜你喜欢

转载自www.cnblogs.com/xingzc/p/9144375.html