Skip to content

Instantly share code, notes, and snippets.

@fge
Created November 5, 2014 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fge/bad48a310da6ba7fc730 to your computer and use it in GitHub Desktop.
Save fge/bad48a310da6ba7fc730 to your computer and use it in GitHub Desktop.
package com.github.fge;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.IntStream;
public final class DigitIntStream
{
// Note: no checking that "number" is >= 0 or base > 0
private static List<Integer> digitsAsList(int number, final int base)
{
final List<Integer> ret = new ArrayList<>();
do {
ret.add(number % base);
number /= base;
} while (number > 0);
return ret;
}
private static IntStream digitsOf(final int number, final int base,
final boolean reversed)
{
final List<Integer> list = digitsAsList(number, base);
if (!reversed)
Collections.reverse(list);
return list.stream().mapToInt(Number::intValue);
}
public static void main(final String... args)
{
digitsOf(12345, 10, false).forEach(System.out::println);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment