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

后端 未结 8 1953
既然无缘
既然无缘 2020-12-01 09:03

I\'ve got a HashSet with a bunch of Integers in it. I want to turn it into an array, but calling

hashset.toArray();
         


        
8条回答
  •  一向
    一向 (楼主)
    2020-12-01 09:42

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

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

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

提交回复
热议问题