lots of exercises in java... from https://github.com/exercism/java

Hexadecimal.java 404B

12345678910111213141516
  1. import java.util.Arrays;
  2. public class Hexadecimal {
  3. private static final String HEXES = "0123456789abcdef";
  4. public static int toDecimal(String input){
  5. boolean isHex = input.matches("^[0-9a-f]+");
  6. if (!isHex) return 0;
  7. return Arrays.stream(input.split(""))
  8. .map(HEXES::indexOf)
  9. .reduce(0, (decimal, hex) -> decimal * 16 + hex);
  10. }
  11. }