import java.util.*;
class Dog {
public Dog(String n) { name = n; }
public String name;
public boolean equals(Object o) {
if((o instanceof Dog) &&
(((Dog)o).name == name)) {
return true;
} else {
return false;
}
}
public int hashCode() {return name.length(); }
}
class Cat { }
enum Pets {DOG, CAT, HORSE }
class MapTest {
public static void main(String[] args) {
Map<Object, Object> m = new HashMap<Object, Object>();
m.put("k1", new Dog("aiko")); // add some key/value pairs
m.put("k2", Pets.DOG);
m.put(Pets.CAT, "CAT key");
Dog d1 = new Dog("clover"); // let's keep this reference
m.put(d1, "Dog key");
m.put(new Cat(), "Cat key");
System.out.println(m.get("k1")); // #1
String k2 = "k2";
System.out.println(m.get(k2)); // #2
Pets p = Pets.CAT;
System.out.println(m.get(p)); // #3
System.out.println(m.get(d1)); // #4
System.out.println(m.get(new Cat())); // #5
System.out.println(m.size()); // #6
}
}
which produces something like this:
Dog@1c
DOG
CAT key
Dog key
null
5
Let's review the output. The first value retrieved is a Dog object (your value
will vary). The second value retrieved is an enum value (DOG). The third value
retrieved is a String; note that the key was an enum value. Pop quiz: What's the
implication of the fact that we were able to successfully use an enum as a key?
The implication of this is that enums override equals() and hashCode(). And,
if you look at the java.lang.Enum class in the API, you will see that, in fact, these
methods have been overridden.
The fourth output is a String. The important point about this output is that the
key used to retrieve the String was made of a Dog object. The fifth output is null.
The important point here is that the get() method failed to find the Cat object
that was inserted earlier. (The last line of output confirms that indeed, 5 key/value
pairs exist in the Map.) Why didn't we find the Cat key String? Why did it work to
use an instance of Dog as a key, when using an instance of Cat as a key failed?
It's easy to see that Dog overrode equals() and hashCode() while Cat didn't.
Let's take a quick look at hashcodes. We used an incredibly simplistic hashcode
formula in the Dog class—the hashcode of a Dog object is the length of the
instance's name. So in this example the hashcode = 6. Let's compare the following
two hashCode() methods:
public int hashCode() {return name.length(); } // #1
public int hashCode() {return 4; } // #2
Time for another pop quiz: Are the preceding two hashcodes legal? Will they
successfully retrieve objects from a Map? Which will be faster?
The answer to the first two questions is Yes and Yes. Neither of these hashcodes
will be very efficient (in fact they would both be incredibly inefficient), but they
are both legal, and they will both work. The answer to the last question is that the
first hashcode will be a little bit faster than the second hashcode. In general, the
more unique hashcodes a formula creates, the faster the retrieval will be. The first
hashcode formula will generate a different code for each name length (for instance
the name Robert will generate one hashcode and the name Benchley will generate
a different hashcode). The second hashcode formula will always produce the same
result, 4, so it will be slower than the first.
分享到:
相关推荐
网上下载的一个哈希表.再次分享一下
哈希映射 hash map hash_map基于hash table(哈希表)。哈希表最大的优点,就是把数据的存储和查找消耗的时间大大降低,几乎可以看成是常数时间;而代价仅仅是消耗比较多的内存。然而在当前可利用内存越来越多的情况...
Linked Hash Map Example java 源码
Hash map implementation in C.
详解JavaScript中Hash Map映射结构的实现_.docx
大数据 Map 哈希 价值五亿美元 Big Data Map hash value of five hundred million U.S. dollars
linux 下hash_map的基本原理及使用,希望对大家有帮助。
hash_map
Hash Map通常在JavaScript中作为一个简单的来存储键值对的地方,不过哈希对象Object并不是一个真正的哈希映射,没Java中的Hash Map来的那么强大,well,接下来带大家详解JavaScript中Hash Map映射结构的实现
A HashMap for Geeks and normal programmers.
关于hash_map的用法与解释: #include <hash_map> #include #include using namespace std; //define the class class ClassA{ public: ClassA(int a):c_a(a){} int getvalue()const { return c_a;} void ...
在使用hash_map的时候,发现他对字符串的支持不是很好,就特写了一个str hash map 的程序,设置和提取键值的性能是hash_map 的20 倍左右。 特意拿出来给大家分享,如果有改进的, 请大家指出。
一个用Hash算法实现的map,可以实际项目所使用。 希望能帮助大家学习。
代码重点是hash_table,附加std::map与其做对比,实现的是一条sql语句:select c_nationkey, c_mktsegment, count(*), max(c_acctbal) from aaa_customer_1g group by c_nationkey, c_mktsegment order by c_...
jg :: dense_hash_map 一个简单的std::unordered_map替代品,具有更好的性能,但失去了稳定的寻址方式,这是一种折衷方案。 在此处查看此哈希图的详细说明: : 生成状态: 特拉维斯(Travis):
具有标准接口的哈希映射,以模板容器的形式实现布谷鸟哈希。
sparse-map一个高效hash map和hash set的C 实现
JAVA实现学生管理系统,可以增删改查操作。该版本可以保存操作,保存的内容在目录下的txt文档中。
(a)可快速查找, 其查找效率等同于hash map的效率. (b)快速遍历, 其遍历效率等同于hash map的效率. (c)树中各节点的Key值必须唯一, 否则将引起层次混乱. 如: 假设树节点的Key值如下 1 2 3 4 1 则Key值为3的...