Performance TreeMap, HashMap and LinkedHashMap?

In TreeMap - Items are sorted
In HashMap - Items are not sorted

So, if I consider the get , put and remove methods that should use the card for performance?

+8
java performance map
source share
2 answers

Use a HashMap if you do not need an order. HashMap faster.

However, you can easily switch using the common interface as an ad:

  Map<String,String> M = new HashMap<String,String>(); ...use M lots of places... 

Then all you have to do is switch one place, and your code uses a new type of card.

Edit:

Simple time test:

 import java.util.*; class TimingTest { public static void main(String[] args) { Map<String,String> M = new HashMap<String,String>(); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { M.put(Integer.toString(i), "foo"); } long end = System.currentTimeMillis(); System.out.println(end - start); } } 
+5
source share

It depends on how fast the hash and comparison functions are on the keys on your map. It depends on whether you are interested in average performance or in the worst case. It depends on whether you have a good hash function applied to your cards; hash values ​​should be well distributed throughout the hash function area (yes, this may depend on your data).

In general (when you can't worry about testing), a hash map is often a good answer, but it can also hardly make a big difference if you don't have thousands of entries (for small sizes, a “VEC map” may also work well).

0
source share

All Articles