首页 / 更多教程 / 手动实现HashMap集合
手动实现HashMap集合
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了手动实现HashMap集合,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2892字,纯文字阅读大概需要5分钟。
内容图文
![手动实现HashMap集合](/upload/InfoBanner/zyjiaocheng/991/3f4f905994ed42c0ab0722200b8debe5.jpg)
/*
* 实现HashMap集合
* put(key,value)
* 1)key-> hash 散列码
* 2)hash& table.length-1 ->index
* 3)if(table[index]==null{
* 直接放
* }else{
* 找key是否存在,如果存在,新值覆盖旧值
* 如果不存在,将key,value封装为一个结点Entry直接添加
*
* }
* */
class MyHashMap<K,V>{
private Entry<K,V>[] table;//桶 用来放节点
private int size;//记录当前节点个数
private static final int defaultCapacity=8;
@Override
public String toString() {
return "MyHashMap{" +
"table=" + Arrays.toString(table) +
'}';
}
class Entry<K,V>{
private K key;
private V value;
protected Entry<K,V> next;
protected int hash;
public Entry(K key, V value, int hash) {
this.key = key;
this.value = value;
this.hash = hash;
}
@Override
public String toString() {
return "Entry{" +
"key=" + key +
", value=" + value +
", hash=" + hash +
'}';
}
}
public MyHashMap(){
this(defaultCapacity);
}
public MyHashMap(int capacity){
table=new Entry[capacity];
}
public int hash(K key){
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
public void put(K key,V value){
int hash=hash(key);//计算散列值
int index=hash & table.length-1;//获取数组下标
if(table[index]==null){
//将key/value封装为一个Entry节点直接放在该位置
table[index]=new Entry<>(key, value, hash);
size++;
}else{
Entry<K,V> firstNode= table[index];
//查找是否有重复key
if(firstNode.key.equals(key)){
//该key存在于该位置的第一个节点//值覆盖
firstNode.value=value;
}else{
//遍历链表
Entry<K,V> currentNode=firstNode;
while(currentNode.next!=null&&!currentNode.key.equals(key)){
currentNode=currentNode.next;
}
if(currentNode.next==null){
//仅剩最后一个元素没有判断
if(currentNode.key.equals(key)){
currentNode.value=value;
}else{
currentNode.next= new Entry<>(key,value,hash);
size++;
}
}else{
// currentNode.next!=null&¤tNode.key.equals(key)
currentNode.value =value;
}
}
}
}
public V get(K key){
//确定散列码
int hash=hash(key);
//确定索引位置
int index=hash&table.length-1;
Entry<K,V> firstNode=table[index];
if(firstNode==null){
return null;
}else{
//遍历列表
Entry<K,V> currentNode=firstNode;
while(currentNode!=null){
if (currentNode.key.equals(key)){
return currentNode.value;
}
currentNode=currentNode.next;
}
}
return null;
}
//移除key所在的entry节点
public boolean remove(K key){
int hash=hash(key);
int index=hash &table.length-1;
Entry<K,V> firstNode=table[index];
if(firstNode==null){
//说明该位置没有元素
return false;
}else{
if(firstNode.key.equals(key)){
table[index]=firstNode.next;//赋值
return true;
}
//firstNode作为当前节点的前一个
while(firstNode.next!=null){//只要用到的引用,都要判断它不为空
if(firstNode.next.key.equals(key)){
firstNode.next=firstNode.next.next;
return true;
}else{
firstNode=firstNode.next;
}
}
}
return false;
}
}
public class HashMapTest {
public static void main(String[] args) {
MyHashMap<String, Integer> map = new MyHashMap<>();
map.put("dandan",23);
map.put("xuanxuan",56);
map.put("mengchen",12);
System.out.println(map);
System.out.println(map.get("dandan"));
System.out.println(map.remove("dandan"));
System.out.println(map);
}
}
内容总结
以上是互联网集市为您收集整理的手动实现HashMap集合全部内容,希望文章能够帮你解决手动实现HashMap集合所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。