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 330 times
Edit
1
Community EvaluationsNo one has reviewed this question yet, be the first!
1
Java code that replaces keys in a template with their values.1
Write a Java implementation of the FizzBuzz code kata.2
Write a function that returns the first character of a string in Java1
A code kata is a small exercice thought to train a certain development competence1
What is the name of the design pattern used to structure complex applications by considering the problem domain?1
What does SRP stand for?4
This code allows to randomly get numbers between 1 to 31 in results. Should have declard SimpleDateFormat in the Thread.