I have a list of integer values (positive, and negative). I need to calculate the percentage of each value. Is there any API in Java to achieve this?
Example 1 -
10, 40, 60, -15, -25, 30, -10, -50, 120
-50 (lowest value) - 0%
-25 - ?
-15 - ?
-10 - ?
10 - ?
120 (highest value) - 100%
Example 2 -
90, 45, 13, -89, -95, -150, 200, 250, 67
-150 (lowest value) - 0% -95 - ? -89 - ? 13 - ? 250 (highest value) - 100%
I'm not sure if you are asking for normalizing based on the index after sorting or the value. But I think based on value makes more sense. For example if your array is 0, 1, 10
, the final percentages will be 0->0%, 1->10%, 10->100%
For this, you will basically do a normalization.
I don't remember the exact syntax but correct it if any syntax is wrong.
int arr[] = {10, 40, 60, -15, -25, 30, -10, -50};
List b = Arrays.asList(ArrayUtils.toObject(arr));
int mn = Collections.min(b);
int mx = Collections.max(b);
int perc_arr = new int[arr.length];
for(int i = 0; i < arr.length; i++)
perc_arr[i] = (arr[i]-mn)/(mx-mn)*100;