HashMap、TreeMap、Hashable和LinkedHashMap

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值