JDK源码阅读:实现自己的HashMap
来源:互联网 发布:文华财经 策略 源码 编辑:程序博客网 时间:2024/05/17 22:50
/*** * 自己实现的一个简单的HashMap<br> * * @author jay * * @param <K> * @param <V> */public class MyHashMap<K, V>{ private Entry[] table; private static int capacity = 16; public MyHashMap() { this(capacity); } public MyHashMap(int capa) { table = new Entry[16]; } static class Entry<K, V> { K key; V value; Entry next; public Entry(K k, V v, Entry ne) { this.key = k; this.value = v; this.next = ne; } public Entry(K k, V v) { this.key = k; this.value = v; this.next = null; } public K getKey() { return key; } public void setKey(K key) { this.key = key; } public V getValue() { return value; } public void setValue(V value) { this.value = value; } public String toString() { if (next == null) return key + "=" + value; else return key + "=" + value + " -> " + next.toString(); } } static final int hash(Object key) { int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); } public V put(K key, V value) { V oldValue = null; int index = (capacity - 1) & hash(key); // int index = Math.abs(key.hashCode() % capacity); if (table[index] == null) table[index] = new Entry<K, V>(key, value); Entry<K, V> pair = new Entry(key, value); boolean found = false; for (Entry e = (Entry) table[index]; e != null; e = e.next) { Entry<K, V> ipair = e; if (ipair.getKey().equals(key)) { oldValue = ipair.getValue(); e.setValue(value); found = true; break; } } if (!found) { Entry entry = (Entry) table[index]; pair.next = entry; table[index] = pair; } return oldValue; } public V get(Object key) { int index = Math.abs(key.hashCode() % capacity); if (table[index] == null) return null; for (Entry e = (Entry) table[index]; e != null; e = e.next) { if (key.equals(e.getKey())) return (V) e.getValue(); } return null; } public String toString() { if (table == null || table.length == 0) return null; StringBuilder builder = new StringBuilder("{\n"); for (int i = 0; i < table.length; i++) { Entry e = table[i]; if (e != null) { builder.append(e.toString() + "\n"); } // for (Entry e = table[i]; e != null; e = e.next) } builder.append("}"); return builder.toString(); } public static void main(String[] ar) { MyHashMap<Integer, String> map = new MyHashMap<Integer, String>(); for (int i = 1; i < 96; i++) map.put(i, "zhangjie_" + i); System.out.println(map.toString()); }}
0 0
- JDK源码阅读:实现自己的HashMap
- JDK源码阅读之HashMap的实现
- JDK源码阅读之 HashMap
- JDK源码阅读之HashMap类
- JDK源码阅读——模拟HashMap
- JDK 1.8 HashMap 源码阅读一
- JDK 1.8 HashMap 源码阅读二
- jdk 1.8 hashmap resize 源码阅读
- [Jdk源码阅读]ArrayList实现
- JDK的HashMap源码解读
- 实现自己的HashMap
- 实现自己的HashMap
- 自己实现的hashmap
- HashMap实现原理以及源码阅读
- JDK源码阅读笔记之——HashMap
- JDK源码阅读——Map(HashMap\TreeMap\LinkedHashMap)
- 【JDK源码阅读8-util】Map接口----HashMap
- JDK源代码阅读:HashMap
- 怎样在自己的网页中嵌入视频?
- Spark性能优化:资源调优篇
- 【网络】select/poll/epoll事件处理图示
- 2016北京传智播客第PHP基础班+就业班(第42期)
- 03-每天一个Linux命令 pwd
- JDK源码阅读:实现自己的HashMap
- ajax 跨域访问
- 软件工程基础学习笔记--软件开发模型
- HTTP协议各个版本的介绍和特点
- ssm项目的搭建(入门级)
- Ext文档里的按钮总结
- OpenStack L版官网配置转载(五)
- 线性回归
- sparkSQL1.1入门之九:sparkSQL之调优