Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 699 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding the max/min value in an array of primitives using Java

#1
It's trivial to write a function to determine the min/max value in an array, such as:

/**
*
* @param chars
* @return the max value in the array of chars
*/
private static int maxValue(char[] chars) {
int max = chars[0];
for (int ktr = 0; ktr < chars.length; ktr++) {
if (chars[ktr] > max) {
max = chars[ktr];
}
}
return max;
}

but isn't this already done somewhere?
Reply

#2
Yes, it's done in the [Collections][1] class. Note that you will need to convert your primitive char array to a Character[] manually.

A short demo:

import java.util.*;

public class Main {

public static Character[] convert(char[] chars) {
Character[] copy = new Character[chars.length];
for(int i = 0; i < copy.length; i++) {
copy[i] = Character.valueOf(chars[i]);
}
return copy;
}

public static void main(String[] args) {
char[] a = {'3', '5', '1', '4', '2'};
Character[] b = convert(a);
System.out.println(Collections.max(Arrays.asList(b)));
}
}

[1]:

[To see links please register here]

Reply

#3
The [Google Guava library][1] has min and max methods in its Chars, Ints, Longs, etc. classes.

So you can simply use:

Chars.min(myarray)

No conversions are required and presumably it's efficiently implemented.

[1]:

[To see links please register here]

Reply

#4
import java.util.Random;

public class Main {

public static void main(String[] args) {
int a[] = new int [100];
Random rnd = new Random ();

for (int i = 0; i< a.length; i++) {
a[i] = rnd.nextInt(99-0)+0;
System.out.println(a[i]);
}

int max = 0;

for (int i = 0; i < a.length; i++) {
a[i] = max;


for (int j = i+1; j<a.length; j++) {
if (a[j] > max) {
max = a[j];
}

}
}

System.out.println("Max element: " + max);
}
}
Reply

#5
Pass the array to a method that sorts it with `Arrays.sort()` so it only sorts the array the method is using then sets *min* to `array[0]` and *max* to `array[array.length-1]`.
Reply

#6
Using Commons Lang (to convert) + Collections (to min/max)

import java.util.Arrays;
import java.util.Collections;

import org.apache.commons.lang.ArrayUtils;

public class MinMaxValue {

public static void main(String[] args) {
char[] a = {'3', '5', '1', '4', '2'};

List b = Arrays.asList(ArrayUtils.toObject(a));

System.out.println(Collections.min(b));
System.out.println(Collections.max(b));
}
}

Note that `Arrays.asList()` wraps the underlying array, so it should not be too memory intensive and it should not perform a copy on the elements of the array.
Reply

#7
The basic way to get the min/max value of an Array. If you need the unsorted array, you may create a copy or pass it to a method that returns the min or max. If not, sorted array is better since it performs faster in some cases.

public class MinMaxValueOfArray {
public static void main(String[] args) {
int[] A = {2, 4, 3, 5, 5};
Arrays.sort(A);
int min = A[0];
int max = A[A.length -1];
System.out.println("Min Value = " + min);
System.out.println("Max Value = " + max);
}
}
Reply

#8
You could easily do it with an `IntStream` and the `max()` method.

### Example

public static int maxValue(final int[] intArray) {
return IntStream.range(0, intArray.length).map(i -> intArray[i]).max().getAsInt();
}

### Explanation

1. `range(0, intArray.length)` - To get a stream with as many elements as present in the `intArray`.

2. `map(i -> intArray[i])` - Map every element of the stream to an actual element of the `intArray`.

3. `max()` - Get the maximum element of this stream as `OptionalInt`.

4. `getAsInt()` - Unwrap the `OptionalInt`. (You could also use here: `orElse(0)`, just in case the `OptionalInt` is empty.)
Reply

#9
Example with float:

public static float getMaxFloat(float[] data) {

float[] copy = Arrays.copyOf(data, data.length);
Arrays.sort(copy);
return copy[data.length - 1];
}

public static float getMinFloat(float[] data) {

float[] copy = Arrays.copyOf(data, data.length);
Arrays.sort(copy);
return copy[0];
}
Reply

#10
You can simply use the new Java 8 [`Stream`s](

[To see links please register here]

) but you have to work with `int`.

The [`stream`](

[To see links please register here]

) method of the utility class [`Arrays`](

[To see links please register here]

) gives you an [`IntStream`](

[To see links please register here]

) on which you can use the [`min`](

[To see links please register here]

) method. You can also do [`max`](

[To see links please register here]

), [`sum`](

[To see links please register here]

), [`average`](

[To see links please register here]

),...

The [`getAsInt`](

[To see links please register here]

) method is used to get the value from the [`OptionalInt`](

[To see links please register here]

)

import java.util.Arrays;

public class Test {
public static void main(String[] args){
int[] tab = {12, 1, 21, 8};
int min = Arrays.stream(tab).min().getAsInt();
int max = Arrays.stream(tab).max().getAsInt();
System.out.println("Min = " + min);
System.out.println("Max = " + max)
}

}

**==UPDATE==**

If execution time is important and you want to go through the data only once you can use the [`summaryStatistics()`](

[To see links please register here]

) method like this

import java.util.Arrays;
import java.util.IntSummaryStatistics;

public class SOTest {
public static void main(String[] args){
int[] tab = {12, 1, 21, 8};
IntSummaryStatistics stat = Arrays.stream(tab).summaryStatistics();
int min = stat.getMin();
int max = stat.getMax();
System.out.println("Min = " + min);
System.out.println("Max = " + max);
}
}

This approach can give better performance than classical loop because the [`summaryStatistics`](

[To see links please register here]

) method is a [reduction operation](

[To see links please register here]

) and it allows parallelization.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through