AT&T; LTE 7:48 AM c @0 91%.-. Write the output that is printed when the given me
ID: 3723773 • Letter: A
Question
AT&T; LTE 7:48 AM c @0 91%.-. Write the output that is printed when the given method below is passed the following map as its parameter. Assume that each parameter map stores its key/value pairs in exactly the order shown, and that is the order in which a for-each loop would examine them. Recall that maps print in a (key1 value1, key2-value2, keyN-valueN) format. Your answer should display the right keys and values in the right order Note that to test your understanding, you can use Java/Eclipse to create a LinkedHashMap which will preserve the order in which the keyX-valueX pairs are added the map. Then you can have the method process the LinkedHashMap you create.) The map: Account Courses 28 Calendar Inbox cotton-shirt, tree-violin, seed-tree, i ght-tree, rain cottonh elp The method: public static void mapMystery1 (Map-String , String map) MapO for (String keymap.keySet)) if lkey.compareTo(map.getikey)) result.put key, map.get (key)) else result.put (map.get (key), key) System.out.println(result);Explanation / Answer
the correct answer is 3rd option i.e
{cotton=rain, light=tree, seed=tree, tree=violin}
Explanation:
In map, values are stored in key-value pair where key is unique attribute. In map, compareTo() method compares the hash value of two object.
lets say object1 and object2,
if hash value of object1 is greater than hash value of object2 it will return integer value greater than 0.
if hash value of object1 is equal to hash value of object2 it will return integer value equal to 0.
if hash value of object1 is less than hash value of object2 it will return integer value less than 0.
Hence in the above example.
> In the first iteration, the hash value of shirt is less than hash value of cotton. hence, it add an entry with key cotton and shirt.
> In second iteration, the hash value of tree is less than hash value of violin., so entry happens for tree as key and violin as value
> In third iteration, the hash value of seed is greater than hash value of tree. so, there is entry of seed as key and tree as value in the map.
Also, map supports unique key only, in fifth iteration, cotton key is overridden with rain value.
====================================================
Please let me know if any doubt or query. Else please rate if you like the solution. Thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.