Skip to main content
Added disclaimer about negative step sizes
Source Link
AJNeufeld
  • 34k
  • 5
  • 39
  • 101

As of Java-9, there exists IntStream.html#iterate(seed, hasNext, next)

If you are trying to get a comma separated list of speeds, use:

String allSpeeds = IntStream.iterate(minKmh, i -> i <= maxKmh, i -> i + step)
                            .mapToObj(Integer::toString)
                            .collect( Collectors.joining( "," ) );

If you just want the integer array of speeds:

int[] speeds = IntStream.iterate(minKmh, i -> i <= maxKmh, i -> i + step).toArray();

Or, if you want the array of speeds as Strings:

String[] allSpeedsAsArray = IntStream.iterate(minKmh, i -> i <= maxKmh, i -> i + step)
                                     .mapToObj(Integer::toString)
                                     .toArray();

Finally, improving @bowman's solution, slightly:

int[] speeds = IntStream.iterate(minKmh, i -> i <= maxKmh, i -> i + step).toArray();

Although, the above solutions require step > 0 or they may not terminate, where as Bowman's solution works fine with negative step sizes.

As of Java-9, there exists IntStream.html#iterate(seed, hasNext, next)

If you are trying to get a comma separated list of speeds, use:

String allSpeeds = IntStream.iterate(minKmh, i -> i <= maxKmh, i -> i + step)
                            .mapToObj(Integer::toString)
                            .collect( Collectors.joining( "," ) );

If you just want the integer array of speeds:

int[] speeds = IntStream.iterate(minKmh, i -> i <= maxKmh, i -> i + step).toArray();

Or, if you want the array of speeds as Strings:

String[] allSpeedsAsArray = IntStream.iterate(minKmh, i -> i <= maxKmh, i -> i + step)
                                     .mapToObj(Integer::toString)
                                     .toArray();

As of Java-9, there exists IntStream.html#iterate(seed, hasNext, next)

If you are trying to get a comma separated list of speeds, use:

String allSpeeds = IntStream.iterate(minKmh, i -> i <= maxKmh, i -> i + step)
                            .mapToObj(Integer::toString)
                            .collect( Collectors.joining( "," ) );

Or, if you want the array of speeds as Strings:

String[] allSpeedsAsArray = IntStream.iterate(minKmh, i -> i <= maxKmh, i -> i + step)
                                     .mapToObj(Integer::toString)
                                     .toArray();

Finally, improving @bowman's solution, slightly:

int[] speeds = IntStream.iterate(minKmh, i -> i <= maxKmh, i -> i + step).toArray();

Although, the above solutions require step > 0 or they may not terminate, where as Bowman's solution works fine with negative step sizes.

Now with variable names from question text
Source Link
AJNeufeld
  • 34k
  • 5
  • 39
  • 101

As of Java-9, there exists IntStream.html#iterate(seed, hasNext, next)

If you are trying to get a comma separated list of speeds, use:

String speedsallSpeeds = IntStream.iterate(lowerminKmh, i -> i <= uppermaxKmh, i -> i + step)
                            .mapToObj(Integer::toString)
                            .collect( Collectors.joining( "," ) );

If you just want the integer array of speeds:

int[] speeds = IntStream.iterate(lowerminKmh, i -> i <= uppermaxKmh, i -> i + step).toArray();

Or, if you want the array of speeds as Strings:

String[] allSpeedsAsArray = IntStream.iterate(lowerminKmh, i -> i <= uppermaxKmh, i -> i + step)
                                     .mapToObj(Integer::toString)
                                     .toArray();

As of Java-9, there exists IntStream.html#iterate(seed, hasNext, next)

If you are trying to get a comma separated list of speeds, use:

String speeds = IntStream.iterate(lower, i -> i <= upper, i -> i + step)
                         .mapToObj(Integer::toString)
                         .collect( Collectors.joining( "," ) );

If you just want the integer array of speeds:

int[] speeds = IntStream.iterate(lower, i -> i <= upper, i -> i + step).toArray();

Or, if you want the array of speeds as Strings:

String[] allSpeedsAsArray = IntStream.iterate(lower, i -> i <= upper, i -> i + step)
                                     .mapToObj(Integer::toString)
                                     .toArray();

As of Java-9, there exists IntStream.html#iterate(seed, hasNext, next)

If you are trying to get a comma separated list of speeds, use:

String allSpeeds = IntStream.iterate(minKmh, i -> i <= maxKmh, i -> i + step)
                            .mapToObj(Integer::toString)
                            .collect( Collectors.joining( "," ) );

If you just want the integer array of speeds:

int[] speeds = IntStream.iterate(minKmh, i -> i <= maxKmh, i -> i + step).toArray();

Or, if you want the array of speeds as Strings:

String[] allSpeedsAsArray = IntStream.iterate(minKmh, i -> i <= maxKmh, i -> i + step)
                                     .mapToObj(Integer::toString)
                                     .toArray();
Included `String[]` as possible result option.
Source Link
AJNeufeld
  • 34k
  • 5
  • 39
  • 101

As of Java-9, there exists IntStream.html#iterate(seed, hasNext, next)

If you are trying to get a comma separated list of speeds, use:

String speeds = IntStream.iterate(lower, i -> i <= upper, i -> i + step)
                         .mapToObj(Integer::toString)
                         .collect( Collectors.joining( "," ) );

If you just want the integer array of speeds:

int[] speeds = IntStream.iterate(lower, i -> i <= upper, i -> i+stepi + step).toArray();

Or, if you want the array of speeds as Strings:

String[] allSpeedsAsArray = IntStream.iterate(lower, i -> i <= upper, i -> i + step)
                                     .mapToObj(Integer::toString)
                                     .toArray();

As of Java-9, there exists IntStream.html#iterate(seed, hasNext, next)

If you are trying to get a comma separated list of speeds, use:

String speeds = IntStream.iterate(lower, i -> i <= upper, i -> i + step)
                         .mapToObj(Integer::toString)
                         .collect( Collectors.joining( "," ) );

If you just want the integer array of speeds:

int[] speeds = IntStream.iterate(lower, i -> i <= upper, i -> i+step).toArray();

As of Java-9, there exists IntStream.html#iterate(seed, hasNext, next)

If you are trying to get a comma separated list of speeds, use:

String speeds = IntStream.iterate(lower, i -> i <= upper, i -> i + step)
                         .mapToObj(Integer::toString)
                         .collect( Collectors.joining( "," ) );

If you just want the integer array of speeds:

int[] speeds = IntStream.iterate(lower, i -> i <= upper, i -> i + step).toArray();

Or, if you want the array of speeds as Strings:

String[] allSpeedsAsArray = IntStream.iterate(lower, i -> i <= upper, i -> i + step)
                                     .mapToObj(Integer::toString)
                                     .toArray();
Source Link
AJNeufeld
  • 34k
  • 5
  • 39
  • 101
Loading