+关注继续查看

Map概览

• HashMap：基于散列表实现，是无序的；
• TreeMap：基于红黑树实现，按Key排序；
• Hashtable：是同步的，与HashMap类似；

HashMap

package simplejava;

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 Q26 {

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 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";
}
}

TreeMap

TreeMap是按key排序的，让我们先看下如下代码，了解其“按key排序”思想。

package simplejava;

import java.util.Map.Entry;
import java.util.TreeMap;

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 Q26 {

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: simplejava.Dog cannot be cast to java.lang.Comparable    at java.util.TreeMap.compare(TreeMap.java:1188)    at java.util.TreeMap.put(TreeMap.java:531)    at simplejava.Q26.main(Q26.java:34)

package simplejava;

import java.util.Map.Entry;
import java.util.TreeMap;

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 Q26 {

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());
}
}

}

HashTable

package simplejava;

import java.util.Map.Entry;

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 Q26 {

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");
for (Entry<Dog, Integer> entry : linkedHashMap.entrySet()) {
System.out.println(entry.getKey() + " - " + entry.getValue());
}
}

}

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

本文转自风一样的码农博客园博客，原文链接：http://www.cnblogs.com/chenpi/p/5505705.html，如需转载请自行联系原作者

TreeMap的使用
TreeMap的使用
0 0

0 0
3. 如何决定使用 HashMap 还是 TreeMap？
3. 如何决定使用 HashMap 还是 TreeMap？
0 0
0 0

0 0

0 0
HashMap VS Hashtable
HashMap与Hashtable的异同点 hashtable是线程安全的，hashmap不是。对线程要求不严格的情况下，hashmap的性能更好。 HashTable不允许null作为key值或value值，Hashmap可以允许一个null作为key值，同时允许多个null作为value值。
589 0
HashTable and HashMap

719 0
+关注