HashMap vs. TreeMap vs. Hashtable vs. LinkedHashMap

HashMap vs. TreeMap vs. Hashtable vs. LinkedHashMap

Map is one of the most important data structures. In this tutorial, I will show you how to use different maps such as HashMap, TreeMap, HashTable and LinkedHashMap.

Map是数据结构中非常重要的一种。在该文章中,我将会告诉你如何去使用不同的map,诸如HashMap,TreeMap,HashTable和LinkedHashMap。

1.Map

There are 4 commonly used implementations of Map in Java SE – HashMap, TreeMap, Hashtable and LinkedHashMap. If we use one sentence to describe each implementation, it would be the following:

This gives us the reason that HashMap should be used if it is thread-safe, since Hashtable has overhead for synchronization.

在JavaSE中有常用的四种Map – HashMap,TreeMap,HashTable和LinkedHashMap。

如果我们用单独的特性去描述不同的实现的话,将在以下列出:

1)HashMap作为一个hash表的实现,它的key和value是无序的。

2)TreeMap是基于红黑树的结构来实现的,并且它的key是有序的。

3)LinkedHashMap保证了插入的顺序。

4)和HashMap相比较,HashTable是同步的。

在这里给了我们一个使用HashMap的理由,因为HashTable是线程安全的,它的同步开销会非常大。

2.HashMap

If key of the HashMap is self-defined objects, then equals() and hashCode() contract need to be followed.

如果我们自定义了HashMap的key对象,那么就需要重写它的equals和hashCode方法,如下:

class Dog {String color; Dog(String c) {color = c;}public String toString(){return color + " dog";}} public class TestHashMap {public static void main(String[] args) {HashMap<Dog, Integer> hashMap = new HashMap<Dog, Integer>();Dog d1 = new Dog("red");Dog d2 = new Dog("black");Dog d3 = new Dog("white");Dog d4 = new Dog("white");hashMap.put(d1, 10);hashMap.put(d2, 15);hashMap.put(d3, 5);hashMap.put(d4, 20);//print sizeSystem.out.println(hashMap.size());//loop HashMapfor (Entry<Dog, Integer> entry : hashMap.entrySet()) {System.out.println(entry.getKey().toString() + " – " + entry.getValue());}}}输出:4white dog – 5black dog – 15red dog – 10white dog – 20 Note here, we add "white dogs" twice by mistake, but the HashMap takes it. This does not make sense, because now we are confused how many white dogs are really there.

注意这里,我们错误的增加了“white dogs”两次,可是HashMap仍然取到了它。这显然没有意义,因为放入很多的“white dogs”会令我们感到混淆。

The Dog class should be defined as follows:

Dog这个类应该定义如下:

class Dog {String color; Dog(String c) {color = c;} public boolean equals(Object o) {return ((Dog) o).color.equals(this.color);} public int hashCode() {return color.length();} public String toString(){return color + " dog";}}

现在的输出是:

3red dog – 10white dog – 20black dog – 15 The reason is that HashMap doesn’t allow two identical elements. By default, the hashCode() and equals() methods implemented in Object class are used. The default hashCode() method gives distinct integers for distinct objects, and the equals() method only returns true when two references refer to the same object. Check outthe hashCode() and equals() contractif this is not obvious to you.

原因是HashMap不允许有两个重复的元素。默认情况下,在使用Object类的时候,hashCode和equals方法就已经被实现了。默认的hashCode方法会针对与不同的对象给予不同的integer的值,并且equals方法则会去参考两个相同对象的引用来返回true。

如果你不熟悉这块的话,可以去看thehashCode() and equals() contract这篇文章。

Check out themost frequently used methods for HashMap, such as iteration, print, etc.

阅读mostfrequently used methods for HashMap这篇文章

3. TreeMap

战胜困难,走出困境,成功就会属于你。

HashMap vs. TreeMap vs. Hashtable vs. LinkedHashMap

相关文章:

你感兴趣的文章:

标签云: