why does the output of the map return keys in lexicographical order? why does it maintain any order like such?
Lexicographical sorting
Because maps store keys with the help of a self balancing BST . And since the data in BST is sorted that’s why the keys are stored in lexicographical order.