map是java中最常用的数据结构之一,在这篇文中,我将说明如何使用不同类型的maps,比如:HashMap,TreeMap,HashTable和LinkedHashMap。
在java SE 中有4个Map常用的实现,分别是HashMap,TreeMap,HashTable和LinkedHashMap。用一句话来描述这四个实分别是:
HashMap是hash table的一个实现,它中的键值是无序的。
TreeMap是基于红黑树结构的一个实现,它是根据key来排序的。
LinkedHashMap保留了插入的顺序。
HashTabe是同步的,与HashMap相比,它有个同步的开销,因此如果程序是线程安全的,那么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 size System.out.println(hashMap.size()); //loop HashMap for (Entry<Dog, Integer> entry : hashMap.entrySet()) { System.out.println(entry.getKey().toString() + " - " + entry.getValue()); } } }
4 white dog - 5 black dog - 15 red dog - 10 white dog - 20
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"; } }
3 red dog - 10 white dog - 20 black dog - 15
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"; } } 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.Comparable at java.util.TreeMap.put(Unknown Source) at collection.TestHashMap.main(TestHashMap.java:35)
class Dog implements Comparable<Dog>{ String color; int size; Dog(String c, int s) { color = c; size = s; } public String toString(){ return color + " dog"; } @Override public 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 - 10 black dog - 15 white dog - 20
white dog - 20 red dog - 10 black dog - 15 white dog - 5
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"; } } 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 - 10 black dog - 15 white dog - 20和使用HashMap不同的是,HashMap的插入顺序是没有被保持的。
red dog - 10 white dog - 20 black dog - 15