如何将 Java HashSet<Integer> 转换为原始 int 数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2451184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:52:02  来源:igfitidea点击:

How can I convert a Java HashSet<Integer> to a primitive int array?

javaarraysinthashset

提问by Hymanbot

I've got a HashSet<Integer>with a bunch of Integersin it. I want to turn it into an array, but calling

我有一个里面HashSet<Integer>有一堆Integers。我想把它变成一个数组,但调用

hashset.toArray();

returns an Object[]. Is there a better way to cast it to an array of intother than iterating through every element manually? I want to pass the array to

返回一个Object[]. int除了手动遍历每个元素之外,是否有更好的方法将其转换为其他数组?我想将数组传递给

void doSomething(int[] arr)

which won't accept the Object[] array, even if I try casting it like

它不会接受 Object[] 数组,即使我尝试像

doSomething((int[]) hashSet.toArray());

采纳答案by Matthew Flaschen

Apache's ArrayUtilshas this (it still iterates behind the scenes):

Apache 的ArrayUtils有这个(它仍然在幕后迭代):

doSomething(ArrayUtils.toPrimitive(hashset.toArray()));

They're always a good place to check for things like this.

他们总是检查此类事情的好地方。

回答by Carl Manaster

Nope; you've got to iterate over them. Sorry.

不; 你必须迭代它们。对不起。

回答by sfussenegger

public int[] toInt(Set<Integer> set) {
  int[] a = new int[set.size()];
  int i = 0;
  for (Integer val : set) a[i++] = val;
  return a;
}

Now that I wrote the code for you it's not that manual anymore, is it? ;)

现在我为你写了代码,它不再是那个手册了,是吗?;)

回答by Shiprack

You could also use the toArray(T[] contents) variant of the toArray() method. Create an empty array of ints of the same size as the HashSet, and then pass it to the toArray() method:

您还可以使用 toArray() 方法的 toArray(T[] contents) 变体。创建一个与 HashSet 大小相同的 int 空数组,然后将其传递给 toArray() 方法:

Integer[] myarray = new Integer[hashset.size()];
doSomething(hashset.toArray(myarray));

You'd have to change the doSomething()function to accept an Integer[]array instead of int[]. If that is not feasible, you'd have convert the array of values returned by toArrayto int[].

您必须更改doSomething()函数以接受Integer[]数组而不是int[]. 如果这不可行,您可以将返回的值数组转换toArrayint[]

回答by Tombart

You can convert a Set<Integer>to Integer[]even without Apache Utils:

你可以转换Set<Integer>Integer[]即使没有阿帕奇utils的:

Set<Integer> myset = new HashSet<Integer>();
Integer[] array = myset.toArray(new Integer[0]);

However, if you need int[]you have to iterate over the set.

但是,如果您需要,则必须int[]迭代该集合。

回答by Ralph Mueller

You can just use Guava's:

你可以只使用番石榴的:

Ints.toArray(Collection<? extends Number> collection)

回答by Jeffrey Bosboom

You can create an int[]from any Collection<Integer>(including a HashSet<Integer>) using Java 8 streams:

您可以使用 Java 8 流int[]从任何Collection<Integer>(包括 a HashSet<Integer>)创建一个:

int[] array = coll.stream().mapToInt(Number::intValue).toArray();

The library is still iterating over the collection (or other stream source) on your behalf, of course.

当然,库仍在代表您迭代集合(或其他流源)。

In addition to being concise and having no external library dependencies, streams also let you go parallel if you have a really big collection to copy.

除了简洁和没有外部库依赖之外,如果你有一个非常大的集合要复制,流还可以让你并行。

回答by sameer

Try this. Using java 8.

尝试这个。使用 Java 8。

    Set<Integer> set = new HashSet<>();
    set.add(43);
    set.add(423);
    set.add(11);
    set.add(44);
    set.add(56);
    set.add(422);
    set.add(34);
    int[] arr = set.stream().mapToInt(Integer::intValue).toArray();