Friday, May 3, 2024
HomeJavaQuiz your self: Unmodifiable Map objects created from Java’s Map.of strategies

Quiz your self: Unmodifiable Map objects created from Java’s Map.of strategies


Oracle Java, Oracle Java Tutorial and Materials, Java Career, Java Jobs, Java Tutorial and Materials

Given the next map

var m = Map.of(1,2,3,4);

Which code fragment is assured to print 37 to the console? Select one.

A. m.forEach((x, y) -> System.out.print(x + y));

 

B. for (var v : m) {

System.out.print(v.getKey() + v.getValue());

}

 

C. for (var v : m.keySet()) {

System.out.print(v + m.get(v));

}

 

D. for (var t : m.entrySet()) {

System.out.print(t.getKey() + t.getValue());

}

 

E. Not one of the above

 

Reply. This query investigates ideas associated to the Map interface and the unmodifiable Map objects created from the Map.of strategies.

Take a look at the impact of the Map.of(1,2,3,4) methodology name. It creates a Map<Integer, Integer> with two key-value pairs. The keys for these pairs are 1 and three, and the corresponding values are 2 and 4. Word that the Map.of strategies that take arguments on this type deal with these arguments as a key, a worth, a key, a worth, and so forth. The keys and values, in fact, might be autoboxed to supply Integer objects.

Since you’ve got two pairs on this Map—1-2 and 3-4—it’s a easy remark of arithmetic that including the key-value pairs will give the outcomes 3 and seven, which appears not less than promising for producing the specified output.

Now, have a look at the code of every choice in flip.

Possibility A has a name to m.forEach. Java 8 added a forEach methodology to many varieties from the Collections API (for instance, java.util.Map or java.util.Checklist). The tactic lets you carry out some motion on every ingredient or entry from the gathering. The map.forEach methodology on this case accepts a BiConsumer<Integer, Integer> as its argument, and the settle for methodology of that argument might be referred to as with a key and worth pair from every key-value pair. So, you’ll be able to see that code probably can print 37. Maintain on to that thought.

Possibility B makes an attempt to make use of the Map in an enhanced for loop. Nevertheless, it will fail as a result of java.util.Map itself doesn’t implement the Iterable interface required for this utilization. From this, you’ll be able to decide that choice B is not going to compile and is subsequently incorrect.

Possibility C extracts a keySet from the Map, which provides you a set of the keys. A Set does implement the Iterable interface; subsequently, you should use it in an enhanced for loop. The physique of the loop extracts the matching worth, provides that worth to the important thing, and prints it. Once more, you’ll be able to see that this code may print 37. Maintain on to this thought too. Possibility D iterates over a Set of Map.Entry objects. That is additionally legitimate and provided that the code prints the sum of the important thing and the worth every time via the loop, it additionally may print 37 to the console. Sure; maintain this thought as nicely.

At this level, you might need observed that choices A, C, and D have been described as may with the ability to print 37, however the query asks for a single reply. Whenever you work on questions for an examination of this sort, it’s vital to choose the right variety of choices. Within the Java examination software program, if a query asks for a single reply, you can be offered with radio buttons, and it’s not possible to pick multiple. This tells you to look a bit more durable.

In truth, choices A, C, and D are all incorrect, and so they all have the identical downside: The iteration of a Map in Java doesn’t present a predictable order. Because of this you can’t be sure that the code will course of the 1-2 pair first and the 3-4 pair second. Consequently, any of those three choices may equally print 37 or 73. As a result of this doesn’t fulfill the query’s question, you’ll be able to see that choice E is appropriate.

As a remaining remark, it’s fascinating that the TreeMap methodology kinds its keys. This may be completed by the keys’ pure order or through the use of a provided Comparator. If the query’s code used a map that ensures order in that method, then choices A, C, and D can be appropriate. To make this variation, declare and initialize the Map utilizing the next:

var m = new TreeMap<>(Map.of(1,2,3,4));

Lastly, you must all the time assume exam-style questions are primarily based on habits you can depend on, as specified within the documentation. Should you take a look at this query’s code utilizing most moderately latest variations of Java, you’ll find that choices A, C, and D will print 37—however that’s luck. Skilled-grade programmers don’t rely upon luck!

Conclusion. The proper reply is choice E.

Supply: oracle.com

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments