Set<Integer> set = Stream.of(Arrays.stream(nums).boxed().parallel().toArray(Integer[]::new)).collect(Collectors.toSet());//nums 为 int 数组
使用并行流是这样嘛?
Set<Integer> set = new HashSet<>(Arrays.asList(nums));
或者这种写法的问题在哪儿 set 没有针对 list 的构造函数?
求指教
1
ztcaoll222 2020-12-13 15:54:59 +08:00 3
Set.of(someArray);
|
2
chenset 2020-12-13 16:18:48 +08:00
haha ..
|
3
weixiaoyun 2020-12-13 17:02:38 +08:00
guava Sets.newHashSet(nums)
|
4
gzk329 OP 额 做算法题的时候用 不导入别的 就 jdk
|
5
gzk329 OP /**
226 * Creates a <i>mutable</i> {@code HashSet} instance containing the given elements. A very thin 227 * convenience for creating an empty set and then calling {@link Iterators#addAll}. 228 * 229 * <p><b>Note:</b> if mutability is not required and the elements are non-null, use {@link 230 * ImmutableSet#copyOf(Iterator)} instead. 231 * 232 * <p><b>Note:</b> if {@code E} is an {@link Enum} type, you should create an {@link EnumSet} 233 * instead. 234 * 235 * <p>Overall, this method is not very useful and will likely be deprecated in the future. 236 */ 237 public static <E> HashSet<E> newHashSet(Iterator<? extends E> elements) { 238 HashSet<E> set = newHashSet(); 239 Iterators.addAll(set, elements); 240 return set; 241 } |
6
yazinnnn 2020-12-13 17:35:17 +08:00
引入 kotlin 库
java 写法 ArraysKt.toSet(arr) kotlin 写法 arr.toSet() 或者 setOf(*arr) |
7
6IbA2bj5ip3tK49j 2020-12-13 17:47:21 +08:00 via iPhone
算法题里用……
算法题里就不该用到这种转换。 |
8
zxCoder 2020-12-13 21:10:35 +08:00
算法题 forforfor 就好了
|
9
cheng6563 2020-12-13 21:31:14 +08:00
并行装个箱这有啥好并的
|
10
gzk329 OP |
11
zxCoder 2020-12-14 08:50:47 +08:00
@gzk329 算法复杂度不是这样算的吧。。。。如果你 for add 进也超时,那无论用什么 api 转 复杂度也是不对的
|
13
samin 2020-12-25 10:28:06 +08:00
我的学习笔记里面有 array to list, 采用你这种做法
https://github.com/SaminZou/study-prj |