How to Convert Int[] to Integer[] in Java

Java 8 way to convert int[][] to Integer[][]

You can do the same thing within a map operation

int[][] twoDimenArray = {};
Integer[][] result = Stream.of(twoDimenArray)
.map(array -> IntStream.of(array).boxed().toArray(Integer[]::new))
.toArray(Integer[][]::new);

Unable to convert from int to Integer in Java

Looks like you've declared your class similar to:

class SizedStack<Integer> {

Integer is now a type variable, not the class java.lang.Integer.

If you want it to be a generic class, write it something like:

class SizedStack<T> {

Or for an Integer-only stack.

class SizedStack {

If it is an Integer-only stack implementing a generic stack.

class SizedStack implements Stack<Integer> {

Converting from int array to Integer arraylist in java

An int[] is quite different from a List<Integer>. For example, an Integer has an identity as well as a value. There is no very simple way to do the conversion.

The following way works with Java 8.

int[] array = {1, 2, 3, 4, 5};
List<Integer> list = IntStream.of(array).boxed().collect(Collectors.toCollection(ArrayList::new));

The following way works in earlier versions.

int[] array = {1, 2, 3, 4, 5};
List<Integer> list = new ArrayList<Integer>();
for (int a : array)
list.add(a);

If you pass an int[] to Arrays.asList you get a List<int[]>, not a List<Integer>.

How to convert Integer[] to int[] array in Java?

You can use Stream APIs of Java 8

int[] intArray = Arrays.stream(array).mapToInt(Integer::intValue).toArray();

Which way of converting int[] to Integer[] is fastest?

Is there any of them that is significantly faster than the rest?

You realize the question you're asking is akin to:

"I have 500,000 screws to screw into place. Unfortunately, I can't be bothered to go out and buy a screwdriver. I do have a clawhammer and an old shoe. Should I use the clawhammer to bash these things into place, or is the shoe a better option?"

The answer is clearly: Uh, neither. Go get a screwdriver, please.

To put it differently, if the 'cost' of converting to Integer[] first is 1000 points of cost in some arbitrary unit, then the difference in the options you listed is probably between 0.01 and 0.05 points - i.e. dwarfed so much, it's irrelevant. Thus, the direct answer to your question? It just does not matter.

You have 2 options:

  1. Performance is completely irrelevant. In which case this is fine, and there's absolutely no point to actually answering this question.

  2. You care about performance quite a bit. In which case, this Integer[] plan needs to be off the table.

Assuming you might be intrigued by option 2, you have various options.

The easiest one is to enjoy the extensive java ecosystem. Someone's been here before and made an excellent class just for this purpose. It abstracts the concept of an int array and gives you all sorts of useful methods, including sorting, and the team that made it is extremely concerned about performance, so they put in the many, many, many personweeks it takes to do proper performance analysis (between hotspot, pipelining CPUs, and today's complex OSes, it's much harder than you might think!).

Thus, I present you: IntArrayList. It has a .sortThis() method, as well as a .sortThis(IntComparator c) method, which you can use for sorting purposes.

There are a few others out there, searching the web for 'java primitive collections' should find them all, if for some reason the excellent eclipse collections project isn't to your liking (NB: You don't need eclipse-the-IDE to use it. It's a general purpose library that so happens to be maintained by the eclipse team).

If you must handroll it, searching the web for how to implement quicksort in java is not hard, thus, you can easily write your own 'sort this int array for me' code. Not that I would reinvent that particular wheel. Just pointing out that it's not too difficult if you must.

How to cast numbers (like int) to Number ?

You do not need to cast it. just use constructor of Number class.

int value = 5;
oracle.jbo.domain.Number num = new oracle.jbo.domain.Number(value);


Related Topics



Leave a reply



Submit