gpt4 book ai didi

java - 是否有模拟 Collections.singleton()/Collections.singletonList()/Collections.singletonMap() 来获取可变集合?

转载 作者:太空狗 更新时间:2023-10-29 22:55:43 29 4
gpt4 key购买 nike

我知道如果我有元素,我想得到 List/Set/Map 我可以调用这个元素:

Collections.singleton()/Collections.singletonList()/Collections.singletonMap()

在这种情况下,我得到了不可变的集合(我无法向集合添加/删除元素)。

是否有模拟获取可变集合?

最佳答案

通过标准库,您可以使用:

new ArrayList<...>(Arrays.asList(...)))
new HashSet<...>(Arrays.asList(...)))

这些可以将任意数量的元素作为参数。像 guava 这样的库有稍微方便一些的方法:

Sets.newHashSet(...)
Lists.newArrayList(...)
Maps.newHashMap(ImmutableMap.of(...))

关于java - 是否有模拟 Collections.singleton()/Collections.singletonList()/Collections.singletonMap() 来获取可变集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25239109/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com