一.基础知识
HashMap 是一个散列表,它存储的内容是键值对(key-value)映射。
HashMap 实现了 Map 接口,根据键的 HashCode 值存储数据,具有很快的访问速度,最多允许一条记录的键为 null,不支持线程同步。
HashMap 是无序的,即不会记录插入的顺序。
HashMap 继承于AbstractMap,实现了 Map、Cloneable、java.io.Serializable 接口。
HashMap 的 key 与 value 类型可以相同也可以不同,可以是字符串(String)类型的 key 和 value,也可以是整型(Integer)的 key 和字符串(String)类型的 value。
Mapname=new HashMap<>();
x为key,可以是整型或字符串;
y为value,可以为整型或字符串,
HashMap 中的元素实际上是对象,一些常见的基本类型可以使用它的包装类。
包装类表:
HashMap在java.util包中,使用时要引用。
import java.util.HashMap.
二.常用方法
1.clear()删除所有的建/值对。
import java.util.HashMap;
class Main {
public static void main(String[] args) {
HashMap sites = new HashMap<>();
sites.put(1, "Google");
sites.put(2, "Runoob");
sites.put(3, "Taobao");
System.out.println("HashMap: " + sites);
// 从HashMap中删除所有映射
sites.clear();
System.out.println("使用 clear() 方法后: " + sites);
}
}
输出结果
HashMap: {1=Google, 2=Runoob, 3=Taobao}
使用 clear() 方法后: {}
2.isEmpty()判断HashMap是否为空。
3.size() 用于计算 hashMap 中键/值对的数量。
4.put() 方法将指定的键/值对插入到 HashMap 中。
import java.util.HashMap;
class Main {
public static void main(String[] args) {
// 创建一个 HashMap
HashMap sites = new HashMap<>();
// 往 HashMap 添加一些元素
sites.put(1, "Google");
sites.put(2, "Runoob");
sites.put(3, "Taobao");
System.out.println("HashMap: " + sites);
}
}
执行结果:
HashMap: {1=Google, 2=Runoob, 3=Taobao}
5.remove() 方法用于删除hashMap 中指定键 key 对应的键值对(key-value)。
hashmap.remove(Object key, Object value);
import java.util.HashMap;
class Main {
public static void main(String[] args) {
HashMap sites = new HashMap<>();
sites.put(1, "Google");
sites.put(2, "Runoob");
sites.put(3, "Taobao");
System.out.println("HashMap: " + sites);
// 删除key为2的映射关系
String siteName = sites.remove(2); // return Runoob
System.out.println("返回值: " + siteName);
System.out.println("删除后的 HashMap: " + sites);
}
}
执行结果
HashMap: {1=Google, 2=Runoob, 3=Taobao}
返回值: Runoob
删除后的 HashMap: {1=Google, 3=Taobao}
6.containsKey() 方法检查 hashMap 中是否存在指定的 key 对应的映射关系。
7.containsValue() 方法检查 hashMap 中是否存在指定的 value 对应的映射关系。
8.replace() 方法替换 hashMap 中是指定的 key 对应的 value。
hashmap.replace(K key, V oldValue, V newValue)
import java.util.HashMap;
class Main {
public static void main(String[] args) {
// 创建一个 HashMap
HashMap sites = new HashMap<>();
// 往 HashMap 添加一些元素
sites.put(1, "Google");
sites.put(2, "Runoob");
sites.put(3, "Taobao");
System.out.println("sites HashMap: " + sites);
// 替换key为2的映射
String value = sites.replace(2, "Wiki");
System.out.println("Replaced Value: " + value);
System.out.println("Updated HashMap: " + sites);
}
}
9.get() 方法获取指定 key 对应对 value。
import java.util.HashMap;
class Main {
public static void main(String[] args) {
// 创建一个 HashMap
HashMap sites = new HashMap<>();
// 往 HashMap 添加一些元素
sites.put(1, "Google");
sites.put(2, "Runoob");
sites.put(3, "Taobao");
System.out.println("sites HashMap: " + sites);
// 得到 value
String value = sites.get(1);
System.out.println("key 1 对应的 value: " + value);
}
}
sites HashMap: {1=Google, 2=Runoob, 3=Taobao}
key 1 对应的 value: Google
10.getOrDefault() 方法获取指定 key 对应对 value,如果找不到 key ,则返回设置的默认值。
hashmap.get(Object key, V defaultValue)
import java.util.HashMap;
class Main {
public static void main(String[] args) {
// 创建一个 HashMap
HashMap sites = new HashMap<>();
// 往 HashMap 添加一些元素
sites.put(1, "Google");
sites.put(2, "Runoob");
sites.put(3, "Taobao");
System.out.println("sites HashMap: " + sites);
// key 的映射存在于 HashMap 中
// Not Found - 如果 HashMap 中没有该 key,则返回默认值
String value1 = sites.getOrDefault(1, "Not Found");
System.out.println("Value for key 1: " + value1);
// key 的映射不存在于 HashMap 中
// Not Found - 如果 HashMap 中没有该 key,则返回默认值
String value2 = sites.getOrDefault(4, "Not Found");
System.out.println("Value for key 4: " + value2);
}
}
Value for key 1: Google Value for key 4: Not Found
三。基础特性
1、输入无限,输出有限的(很大)
2.相同的输入,有相同的输出
3.不同的输入,可能有相同的输出(哈希冲突)
4.分布具有均匀性(mod一个数后分布也均匀)
比如 用此园代表哈希的输出范围,其上相同面积的value个数差不多
即图中三个小圈对应的值差不多。
四.简单应用
1.在超多数据中寻找出现次数最多的数。
显然不能直接对所有数据用哈希,内存不够。
哈希表空间的使用,只和不同数的种类有关。同样的数value+1;
方法利用mod将所有的数据分为十份,对每一份使用hash求最多的数。
2.哈希表实现原理
数组加链表,查找是O(1)常数级。
扩容次数O(logN),每次扩容承担的单价O(N)。
则每次增删该查O(N*logN)/N。
Java中可以使用离线扩容减少时间。
3.设计RandomPool结构
要求1.insert(key):将某个key加入到该结构中,做到不重复加入。
2.delete(key):将原本在结构中的某个key移除
3.getRandom():等概率随机返回结构中的一个key。
时间复杂度O(1).
public class Problem{
public static class Pool{
private HashMap ketIndexMap;
private HashMap indexKeyMap;
private int size;
public Pool(){
this.keyIndexMap=new HashMap();
this.indexKeyMap=new HashMap();
this.size=0;
}
public void insert(K key){
if(!this.keyIndexMap.containsKey(key)){
this.keyIndexMap.put(key,this.size);
this.indexKeyMap.put(this.size++,key);
}
public void delete(K key){
if(this.keyIndexMap.containsKey(key)){
int deleteIndex=this.keyIndexMap.get(key);
int lastIndex=--this.size;
K lastKey=this.indexKeyMap.get(lastIndex);
this.keyIndexMap.put(lastKey,deleteIndex);
this.indexKeyMap.put(deleteIndex,lastKey);
this.keyIndexMap.remove(key);
this.indexKeyMap.remove(lastIndex);
}
}
public K getRandom() {
if(this.size==0){
return null;
}
int randomIndex=(int)(Math.random()*this.size);
return this.indexKeyMap.get(randomIndex);
}
}
}
4.布隆过滤器
100亿个黑名单判断。
黑->白 (不会有)
白->黑(有可能,很低)
(位图)
待续。。。。
来源:菜鸟教程
左课整理



