-2

I'm getting an error when filtering an array

The operator % is undefined for the argument type(s) int[], int

How do I get the iteration value using Java 8?

int[] values = new int[9000000];
Random random = new Random();

for (int i = 0; i < values.length; i++) {
    values[i] = random.nextInt(9000000) + 1;
}

long counter = Stream.of(values)
    .filter(x - > x % 3 == 0) // Error here
    .count();
System.out.println(counter);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Tiago Silva
  • 229
  • 2
  • 9

1 Answers1

1

Use Arrays.stream or IntStream.of instead. Stream.of is a generic method, which interprets the array as one element (Java generics do not support primitives).

long counter = IntStream.of(values)
  .filter(x -> x % 3 == 0)
        .count();
Unmitigated
  • 76,500
  • 11
  • 62
  • 80