Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion of collection to int array

I want to display a graph and for displaying that i need integer values.I get this from my code

    Collection c = Sort.values();

Is there any way that i convert collection in such a way that i get integer values?i get this when i print the collection c

    [64770, 26529, 13028, 848, 752, 496]
like image 213
Xara Avatar asked May 19 '12 11:05

Xara


4 Answers

The question was: conversion to int array
Integer[] can not be assigned to int[] or vice versa

int[] array = c.stream().mapToInt( i -> i ).toArray();
like image 68
Kaplan Avatar answered Oct 17 '22 03:10

Kaplan


Assuming the values are of type Integer, you can try this:

Collection c = Sort.values();
Integer[] a = (Integer[])(c.toArray(new Integer[c.size()]));
like image 45
Sergey Kalinichenko Avatar answered Oct 17 '22 01:10

Sergey Kalinichenko


for (Integer value : c) {
    int i = value.intValue();
    //do something with either value or i
}
like image 3
Mattias Isegran Bergander Avatar answered Oct 17 '22 02:10

Mattias Isegran Bergander


Simply:

Integer[] yourArrayVar = yourCollectionVar.toArray(new Integer[0]);

java just needs to know what kind of array to produce.

like image 1
Amr Lotfy Avatar answered Oct 17 '22 02:10

Amr Lotfy