1.Map概览
Java SE中有四种常见的Map实现——HashMap,TreeMap,Hashtable,LinkedHashMap。如果我们使用一句话来概括它们的特点,就是:
HashMap就是一张hash表,键和值都没有排序。
TreeMap以红-黑树结构为基础,键值按顺序排序
LinkedHashMap保存了插入时的顺序。
Hashtable是同步的(而HashMap是不同步的)。所以如果在线程安全的环境下应该多使用
HashMap,而不是hashtable,因为Hashtable对线程的同步有额外的开销
2.HashMap
如果HashMap的键(key)是自定义的对象,那么需要按规则定义它的equals()和hashCode()方法
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);
System.out.println(hashMap.size());
for(Map.Entry<Dog,Integer> entry:hashMap.entrySet()){
System.out.println(entry.getKey().toString() + " - "
+ entry.getValue());
}
}
}
class Dog{
String color;
Dog(String c){
color=c;
}
public String toString(){
return color+" dog";
}
}
输出:
4
white dog - 5
black dog - 15
red dog - 10
white dog - 20
注意,我们错误的将"white dogs"添加了两次,但是HashMap却接受了两只" white dogs"。这不合理(因为HashMap的键不应该重复),我们会搞不清楚真正有多少白色的存在。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";
}
}
现在输出结果如下:
3
red dog - 10
white dog - 20
black dog - 15
输出结果如上是因为HashMap不允许有两个相等的元素存在。默认情况下(也就是类没有实现hashCode()和equals()方法时),会使用Object类中的这两个方法。Object类中的hashCode()对于不同的对象会返回不同的整数,而只有两个引用指向的同样的对象时equals()才会返回true。
3. TreeMap
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.Comparable
at java.util.TreeMap.put(Unknown Source)
at collection.TestHashMap.main(TestHashMap.java:35)
因为TreeMap按照键的顺序进行排列对象,所以键的对象之间需要能够比较,所以就要实现
Comparable接口。你可以使用String作为键,String已经实现了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";
}
@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
结果根据键的排列顺序进行输出,在我们的例子中根据size排序的。
如果我们将“Dog d4 = new Dog(“white”, 10);”替换成“Dog d4 = new
Dog(“white”, 40);”,那么输出会变成:
white dog - 20
red dog - 10
black dog - 15
white dog - 5
这是因为TreeMap使用compareTo()方法来比较键值的大小,size不相等的狗是不同的狗。
4. Hashtable
Java文档写到:
HashMap类和Hashtable类几乎相同,不同之处在于HashMap是不同步的,也不允许接受null键和null值。
5. LinkedHashMap
LinkedHashMap is a subclass of HashMap. That means it inherits the features of HashMap. In addition, the linked list preserves the insertion-order.
Let’s replace the HashMap with LinkedHashMap using the same code used for HashMap.
LinkedHashMap是HashMap的子类,所以LinkedHashMap继承了HashMap的一些属性,他在HashMap基础上增加的特性就是保存了插入对象的顺序。
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 - 10
white dog - 20
black dog - 15
分享到:
相关推荐
HashMap,HashTable,LinkedHashMap,TreeMap的区别
java中HashMap,LinkedHashMap,TreeMap,HashTable的区别
主要介绍了HashMap vs TreeMap vs Hashtable vs LinkedHashMap的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
对基础的掌握尤其重要,其中List,Set,Map的作用以及使用的场景和分类描述,其中Arraylist 与 LinkedList 区别,HashSet与TreeSet与LinkedHashSet对⽐,LinkedHashMap和HashMap,TreeMap对⽐,HashMap 和 Hashtable...
Map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等。 TreeMap:基于红黑树(Red-Black tree)的 NavigableMap 实现,该映射根据其键的自然顺序进行排序,或者根据创建映射...
Hashtable TreeMap LinkedHashMap 我们主要来说说最常用的HashMap: 添加元素:put(key,value) HashMap map = new HashMap<>(); map.put("name", "大妖怪"); map.put("age", "100"); 复制代码 查询元素:get(key...
Map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等。这篇文章主要介绍了Java Map 按照Value排序的实现方法,需要的朋友可以参考下
Java集合详解4:HashMap和HashTable Java集合详解5:深入理解LinkedHashMap和LRU缓存 Java集合详解6:TreeMap和红黑树 Java集合详解7:HashSet,TreeSet与LinkedHashSet Java集合详解8:Java集合类细节精讲 JavaWeb
HashMap LinkedHashMap TreeMap WeakHashMap Hashtable Collection 集合体系详解 Set 接口 AbstractSet 抽象类SortedSet 接口HashSet LinkedHashSet TreeSet List 接口 AbstractList 和 AbstractSequentialList ...
LinkedHashMap ConcurrentHashMap TreeMap Hashtable Set源码系列 HashSet LinkedHashSet TreeSet HashSet Concurrent源码系列 待完善 JVM(Java虚拟机) 类加载 垃圾回收算法 JavaConcurrent(Java并发系列) 【Java...
对比:Hashtable、HashMap、LinkedHashMap、ConcurrentHashMap、TreeMap (看第六条就可以) HashMap 用什么数据结构实现的 加载因子是什么 HashMap 初始化传入的容量参数的值是就是 HashMap 实际分配的空间么 ...
HashTable和HashMap的区别详解 LeetCode 27. 删除元素(Remove Element) 7月8号 7月5号 复习hashMap concurrentHashmap [LeetCode 70. 爬楼梯(Climbing Stairs).md](Java基础/数据结构与算法/LeetCode/LeetCode ...
interface Map<Key> TreeMap<K> HashMap<K> LinkedHashMap<K> Hashtable<K> extends Dictionary<K> - JDK 1.0 接口 Set TreeSet HashSet 树特里================== 问题: 如何使用两个堆栈实现队列? 答案:...
复杂度 时间复杂度 空间复杂度 线性数据结构动态数组(ArrayList) 链表(LinkedList) 单向链表 双向链表 循环链表 ...映射(HashMap、LinkedHashMap) 二叉堆(BinaryHeap) 优先级队列(PriorityQueue)
- HashMap - LinkedHashMap - TreeMap - WeakHashMap - Hashtable - Collection 集合体系详解 - Set 接口 - AbstractSet 抽象类 - SortedSet 接口 - HashSet - LinkedHashSet - TreeSet - List 接口 -...
Simple Java 是 Java ...LinkedHashMap 按值排序地图 高效计数器 HashMap 常用方法。例如,按值排序 深入理解Arrays.sort(T[], Comparator < ? super T > c) 常见排序,Collections、Arrays、Tre
LinkedHashMap和TreeMap 4. 12 对Map排序 4. 13 Properties属性文件 第5章 字符串 5. 1 使用String 5. 2 基本数据类型与字符串的转化 5. 3 判断Java标识符 5. 4 使用StringBuffer ...
java为数据结构中的映射定义一个接口java.util.Map,有四个实现类HashMap Hashtable LinkedHashMap TreeMap用法和区别;对Map排序; 5字符串 使用String;判断一个字符串是否是合法的java标识符;使用StringBuffer;...
HashMap (done) Hashtable (done) Properties (done) LinkedHashMap (done) IdentityHashMap (done) TreeMap (done) WeakHashMap (done) 并发集合 ArrayBlockingQueue(done) LinkedBlockingDeque (done) ...
14.7.5 SortedMap接口与TreeMap类 305 14.7.6 映射的遍历 308 14.8 栈在Java中的实现 309 14.8.1 Stack类的使用 309 14.8.2 Deque接口的使用 310 14.8.3 利用栈计算数学表达式 311 14.9 集合元素的常用...