How to filter negative values from a set of arrays? I just want to get the positive values, is there any specific class to do it in Java? Is the Math.max in Java is the correct class to do it?
Java 8+
You can use Stream and lambda expressions:
Integer[] numbers = {1, -5, 3, 2, -4, 7, 8};
Integer[] positives = Arrays.asList(numbers)
.stream()
.filter(i -> i > 0) // >= to include 0
.toArray(Integer[]::new);
System.out.println(Arrays.asList(positives));
Output:
[1, 3, 2, 7, 8]
Is the Math.max in Java is the correct class to do it?
Math is class and Math.max() is static method ,
You just simply need to check each element against the condition
if(number < 0 ){
//negative
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With