Complexity of Treemap insertion vs HashMap insertion

Viewed 45338

I am confused with the time complexity of these two algorithms.

//time complexity O(nlog(n))
public void usingTreeMap(){
    Map<Integer, Integer> map = new TreeMap<Integer, Integer>();
    for (int i = 0; i < 10; i++) {
        map.put(i, i);
    }
}
//time complexity O(n)
public void usingHashMap(){
    Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    for (int i = 0; i < 10; i++) {
        map.put(i, i);
    }
}

Is the time complexity to the usingTreeMap algorithm correct.I do know in treemap the insertion time is log(n) but if we iterate over an array of 10 elements does it become nlog(n).

4 Answers
Related