HashMap vs. TreeMap vs. Hashtable vs. LinkedHashMap

来源:互联网 发布:数组中重复次数最多的 编辑:程序博客网 时间:2024/04/28 15:04



Map是最重要的数据结构。这篇文章中,我会带你们看看HashMap, TreeMap, HashTable和LinkedHashMap的区别。

1. Map概览

 

  1. HashMap


 

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
 Dog类应该定义如下:
class Dog {String color; Dog(String c) {color = c;public boolean equals(Object o) {return ((Dog) o).color == this.color;public int hashCode() {return color.length();public String toString(){return color + " dog";}}现在输出结果如下:
3red dog - 10white dog - 20black dog - 15

 

3. TreeMap


 

class Dog {String color; Dog(String c) {color = c;}public boolean equals(Object o) {return ((Dog) o).color == this.color;public int hashCode() {return color.length();}public String toString(){return color + " dog";}} public class TestTreeMap {public static void main(String[] args) {Dog d1 = new Dog("red");Dog d2 = new Dog("black");Dog d3 = new Dog("white");Dog d4 = new Dog("white"); TreeMap<Dog, Integer> treeMap = new TreeMap<Dog, Integer>();treeMap.put(d1, 10);treeMap.put(d2, 15);treeMap.put(d3, 5);treeMap.put(d4, 20); for (Entry<Dog, Integer> entry : treeMap.entrySet()) {System.out.println(entry.getKey() + " - " + entry.getValue());}}}输出:
Exception in thread "main" java.lang.ClassCastException: collection.Dog cannot be cast to java.lang.Comparableat java.util.TreeMap.put(Unknown Source)at collection.TestHashMap.main(TestHashMap.java:35)

 

我们来修改下Dog类,让它实现Comparable接口。
 

class Dog implements Comparable<Dog>{String color;int size; Dog(String c, int s) {color = c;size = s;public String toString(){return color + " dog";} @Overridepublic int compareTo(Dog o) {return  o.size - this.size;}} public class TestTreeMap {public static void main(String[] args) {Dog d1 = new Dog("red", 30);Dog d2 = new Dog("black", 20);Dog d3 = new Dog("white", 10);Dog d4 = new Dog("white", 10); TreeMap<Dog, Integer> treeMap = new TreeMap<Dog, Integer>();treeMap.put(d1, 10);treeMap.put(d2, 15);treeMap.put(d3, 5);treeMap.put(d4, 20); for (Entry<Dog, Integer> entry : treeMap.entrySet()) {System.out.println(entry.getKey() + " - " + entry.getValue());}}}输出:
red dog - 10black dog - 15white dog - 20

 

如果我们将“Dog d4 = new Dog(“white”, 10);”替换成“Dog d4 = new Dog(“white”, 40);”,那么输出会变成:
 

white dog - 20red dog - 10black dog - 15white dog - 5

 

4. Hashtable

 

5. LinkedHashMap

 

 

class Dog {String color; Dog(String c) {color = c;public boolean equals(Object o) {return ((Dog) o).color == this.color;public int hashCode() {return color.length();public String toString(){return color + " dog";}} public class TestHashMap {public static void main(String[] args) { Dog d1 = new Dog("red");Dog d2 = new Dog("black");Dog d3 = new Dog("white");Dog d4 = new Dog("white"); LinkedHashMap<Dog, Integer> linkedHashMap = new LinkedHashMap<Dog, Integer>();linkedHashMap.put(d1, 10);linkedHashMap.put(d2, 15);linkedHashMap.put(d3, 5);linkedHashMap.put(d4, 20); for (Entry<Dog, Integer> entry : linkedHashMap.entrySet()) {System.out.println(entry.getKey() + " - " + entry.getValue());}}}
输出:

 

red dog - 10black dog - 15white dog - 20
如果我们使用HashMap的话,输出将会如下,会打乱插入的顺序:

red dog - 10white dog - 20black dog - 15
0 0
原创粉丝点击