Hard
This code
public class Numerals {
private static final NavigableMap<Integer, String> CONVERSIONS = buildConversions();
private Numerals() {}
public static String toRoman(int arabic) {
return highestKnownConversion(arabic).map(toRomanRepresentation(arabic)).orElse("");
}
private static Optional<Entry<Integer, String>> highestKnownConversion(int arabic) {
return Optional.ofNullable(CONVERSIONS.floorEntry(arabic));
}
private static Function<Entry<Integer, String>, String> toRomanRepresentation(int arabic) {
return conversion -> conversion.getValue() + toRoman(arabic - conversion.getKey());
}
private static NavigableMap<Integer, String> buildConversions() {
NavigableMap<Integer, String> conversions = new TreeMap<>();
conversions.put(1, "I");
conversions.put(4, "IV");
conversions.put(5, "V");
conversions.put(9, "IX");
conversions.put(10, "X");
return conversions;
}
}
Author: Clément DevosStatus: PublishedQuestion passed 321 times
Edit
1
Community EvaluationsNo one has reviewed this question yet, be the first!
Similar QuestionsMore questions about Java
4
This code allows to randomly get numbers between 1 to 31 in results. Should have declard SimpleDateFormat in the Thread.2
Write a function that returns the first character of a string in Java1
Java code that replaces keys in a template with their values.1
Write a Java implementation of the FizzBuzz code kata.1
A code kata is a small exercice thought to train a certain development competence