Way1:
TreeMap<Integer, String> map = new TreeMap<Integer, String>();
int highestKey = map.lastKey();
Source Code of TreeMap.lastKey() from JDK:
Entry<K,V> p = root;
if (p != null)
while (p.right != null)
p = p.right;
return p;
Way2:
TreeMap<Integer, String> map = new TreeMap<Integer, String>();
Object[] keys = map.keySet().toArray();
int highestKey = keys[keys.length - 1]
When you look into the implementation of the lastKey() we see that we have a while loop. I think time complexity for this is O(n).
This makes me think that Way2 is better or efficient way to implement in terms of time complexity. Is my understanding correct ? Which is the better way to use.