Map是最重要的数据结构之一。开始会告诉怎么用HashMap、TreeMap、Hashtable和LinkedHashMap
1、Map概述
在Java SE中有4种Map的实现:HashMap、TreeMap、Hashtable和LinkedHashMap.
HashMap:用哈希表实现,键和值无序
TreeMap:用红黑树实现,键是有序的
LinkedHashMap:维护插入顺序
Hashtable:与HashMap比较是同步
2、HashMap
HashMap的键是自定对象,equals()和hashCode()需要实现
import java.util.HashMap;
import java.util.Map.Entry;
class Dog {
String color;
Dog(String c) {
color = c;
}
public String toString(){
return color + " dog";
}
}
public class Main {
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
red dog - 10
black dog - 15
white dog - 20
white dog - 5
注意到,添加两次white dogs,但是HashMap仍然加进去了。这样没有意义,因为现在容易混淆真正的有多少white dogs
import java.util.HashMap;
import java.util.Map.Entry;
class Dog {
String color;
Dog(String c) {
color = c;
}
public String toString(){
return color + " dog";
}
public boolean equals(Object o)
{
return ((Dog)o).color == this.color;
}
public int hashCode()
{
return color.length();
}
}
public class Main {
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());
}
}
}
输出为:
3
red dog - 10
white dog - 20
black dog - 15
原因是HashMap不允许有同一的元素。默认情况下,hashCode()和equals()是用的超类Object的方法。默认的hashCode()方法是对不同的对象赋一个不同的值。equals()是如果两个引用指向的是同一个对象就认为是相等,返回true
3、TreeMap
TreeMap是根据键排序的。
import java.util.TreeMap;
import java.util.Map.Entry;
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 Main {
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: Dog cannot be cast to java.lang.Comparable
at java.util.TreeMap.put(Unknown Source)
at Main.main(Main.java:31)
因为TreeMap是键排序的,键对象必须是可比较的。也是为什么要实现Comparable接口。
import java.util.TreeMap;
import java.util.Map.Entry;
class Dog implements Comparable<Dog>{
String color;
int size;
Dog(String c, int s) {
color = c;
size = s;
}
public String toString(){
return color + " dog";
}
public boolean equals(Object o) {
return ((Dog) o).color == this.color;
}
public int compareTo(Dog b)
{
return b.size - this.size;
}
}
public class Main {
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
4、Hashtable
HashMap与Hashtable相当一致,除了异步和允许null
5、LinkedHashMap
LinkedHashMap是HashMap的子类,意味着继承了HashMap的特征。另外,维持了插入的顺序
import java.util.TreeMap;
import java.util.Map.Entry;
import java.util.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 Main {
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