Created
November 5, 2014 04:49
-
-
Save fge/bad48a310da6ba7fc730 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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