Given the code fragment:
What is the result?
Given the code fragment:
What is the result?
The code creates two maps based on the symbols and exchange rates provided. The first map (map1) converts the symbols to an exchange rate as a reciprocal value and does not preserve any specific order. The second map (map2) sorts these entries by the natural order of the keys (symbol strings) and collects them into a LinkedHashMap, which maintains this order during iteration. The final output reflects this sorted order: CNY -> 6.42, EUR -> 0.84, GBP -> 0.75, USD -> 1.00.
C is Correct
This compiles fine
Correct answer: C
C is the correct answer
C is correct 1- first map not considered order. (stream) {EUR=0.8354916868577157, GBP=0.7544322897019993, USD=1.0, CNY=6.418098009491084} 2- second map sorted by natural order of the key and uses linkedhash, then output format values to two decimal CNY -> 6.42 EUR -> 0.84 GBP -> 0.75 USD -> 1.00
notice: value is not important in this case, only key
class Test { public static void main(String[] args) throws IOException { var symbols = List.of("USD", "GBP", "EUR", "CNY"); var exchangeRate = List.of(1.0, 1.3255, 1.1969, 0.1558094); var map1 = IntStream.range(0, Math.min(symbols.size(), exchangeRate.size())).boxed() .collect(Collectors.toMap(i -> symbols.get(i), i-> 1.0 / exchangeRate.get(i))); var map2 = map1.entrySet().stream() .sorted(Map.Entry.comparingByKey()) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new)); map2.forEach((var k, var v) -> System.out.printf("%s -> %.2f\n", k, v)); } } -> C
run: CNY -> 6,42 EUR -> 0,84 GBP -> 0,75 USD -> 1,00 BUILD SUCCESSFUL (total time: 0 seconds)
C is correct