I was playing around with a TreeMap
and decided to do a simple performance test by comparing System.currentTimeMillis()
. The results were quite unexpected: after some more testing I found out that a TreeMap
with a Comparator
took very long to be created. I have the following code:
public static void main(String[] args) {
final long millis1 = System.currentTimeMillis();
final TreeMap<Integer,Integer> natural = new TreeMap<>();
final long millis2 = System.currentTimeMillis();
System.out.println(millis2 - millis1);
final TreeMap<Integer,Integer> compare = new TreeMap<>(Integer::compare);
System.out.println(System.currentTimeMillis() - millis2);
}
Output:
0
114
Is this normal? Why does this happen?