俗语有云,欲善其事,必利其器。
于代码而言,多看高手代码,多看经典代码,才是提升的不二法门,最近发觉基础薄弱,于是细细专研jdk的经典代码实现,细节先不表,老夫看代码,向来看得爽了,自己撸袖子开干!
以下为代码:权为练手之用,细节尚未雕琢。
package jdk.collection.map;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
*
* use this class to know how hash map builds,and how to hanler hash collision
* <p>
* <li>how to store data
* <li>how hash collision solves
*
* @version 1.0.0,2017-3-18
* @author xiongyouzheng
* @since 1.0.0
*/
public class HashMap<K, V> implements Map<K, V> {
public static final int CAPACITY = 20;
// buckets use to save data
/**
* when you got 'K' then hash it ,find the right position to store it and when the position has element already,then
* use collisition method to store it this default implention used chaining method
*/
private MyEntry[] table = new MyEntry[CAPACITY];
// private String[] data=new ArrayList<String>();
public HashMap() {
init();
}
@Override
public int size() {
int size = 0;
for (int i = 0; i < CAPACITY; i++) {
if ((null != table[i]) && (!table[i].isEmpty())) {
size += table[i].size();
}
}
return size;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean containsKey(Object key) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean containsValue(Object value) {
// TODO Auto-generated method stub
return false;
}
@Override
public V get(Object key) {
// so now use hash method to get the key from the table
// first of all,get key's hascode,then get its position of the array,finially search linkedlist of each node
int position = hash(key.hashCode());// use hash method get the postion of the key
MyEntry<K, V> linkeEntry = table[position];
return linkeEntry.getValue((K) key);
}
private int hash(int h) {
return h % CAPACITY;// MOD取值法
/*
* h ^= (h >>> 20) ^ (h >>> 12); return h ^ (h >>> 7) ^ (h >>> 4);
*/
}
@Override
public V remove(Object key) {
// TODO Auto-generated method stub
return null;
}
@Override
public void putAll(Map<? extends K, ? extends V> m) {
// TODO Auto-generated method stub
}
@Override
public void clear() {
// TODO Auto-generated method stub
}
@Override
public Set<K> keySet() {
// TODO Auto-generated method stub
return null;
}
@Override
public Collection<V> values() {
// TODO Auto-generated method stub
return null;
}
@Override
public Set<java.util.Map.Entry<K, V>> entrySet() {
// TODO Auto-generated method stub
return null;
}
@Override
public V put(K key, V value) {
int position = hash(key.hashCode());// use hash method get the postion of the key
MyEntry<K, V> linkeEntry = table[position];
putData(linkeEntry, key, value);
return null;
}
public void init() {
for (int i = 0; i < CAPACITY; i++) {
MyEntry<K, V> myEntry = new MyEntry<K, V>();
myEntry.setHead(new Node<K, V>());
table[i] = myEntry;
}
}
/**
* now linkedList contains all the data with same key,i should set value to the right position
*/
@SuppressWarnings("unchecked")
private void putData(MyEntry<K, V> linkeEntry, K key, V value) {
Node<K, V> head = linkeEntry.getHead();
if (head.isEmpty()) {// if totally empty link
head = new Node<K, V>(key, value);// initiate it
head.next = null;
linkeEntry.setHead(head);// initiate head
return;
}
// Node<K, V> tail = head;
if (null == head.next) {
// only one element
if (head.getKey() == key) {// find the same ele,such as you put("A",A),then you put("A",N),so
// update it
head.setValue(value);
return;
}
}
while (null != head.next) {
if (head.getKey() == key) {// find the same ele,such as you put("A",A),then you put("A",N),so
// update it
head.setValue(value);
return;
} else {
head = head.next;
}
}
head.setNext(new Node<K, V>(key, value));
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("{");
for (int i = 0; i < CAPACITY; i++) {
if (!table[i].isEmpty()) {
stringBuilder.append(table[i].elementsString());
}
}
return stringBuilder.append("}").toString();
}
}
class Node<K, V> {
V value;
K key;
Node<K, V> next;
public Node() {
// TODO Auto-generated constructor stub
}
public void setEle(K key, V value) {
this.key = key;
this.value = value;
this.next = null;
}
public Node(K key, V value) {
this.key = key;
this.value = value;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public Node<K, V> getNext() {
return next;
}
public void setNext(Node<K, V> next) {
this.next = next;
}
public boolean isEmpty() {
return null == key && value == null && null == next;
}
@Override
public String toString() {
return "Node [value=" + value + ", key=" + key + ", next=" + next + "]";
}
}
class MyEntry<K, V> implements Iterable {
Node<K, V> head;
Node<K, V> getHead() {
return head;
}
public int size() {
int number = 0;
if (null == head) {
return 0;
}
if (null == head.next) {
if (head.isEmpty()) {
return 0;
} else {
return 1;
}
}
while (null != head) {
if (!head.isEmpty()) {
number++;
}
head = head.next;
}
return number;
}
V getValue(K key) {
if (null == head) {
return null;
}
if (null == head.next) {
if (head.getKey().equals(key)) {// find the data
return head.getValue();
}
}
while (null != head.next) {
if (!head.isEmpty()) {
if (head.getKey().equals(key)) {// find the data
return head.getValue();
} else {
head = head.next;
}
}
head = head.next;
}
return null;
}
String elementsString() {
StringBuilder builder = new StringBuilder();
if (null == head.next) {
return builder.append(head.getKey()).append("=").append(head.getValue()).append(",").toString();
}
Node<K, V> tail = head;
do {
if (!tail.isEmpty()) {
builder.append(tail.getKey()).append("=").append(tail.getValue()).append(",").toString();
}
tail = tail.next;
} while (null != tail);
/*
* while (null != head.next) { if (!head.isEmpty()) {
* builder.append(head.getKey()).append("=").append(head.getValue()).append(",").toString(); } head = head.next;
* }
*/
return builder.toString();
}
boolean contains(K key) {
return false;
}
@Override
public Iterator iterator() {
// TODO Auto-generated method stub
return new EntryIterator();
}
public void setHead(Node<K, V> head) {
this.head = head;
}
public boolean isEmpty() {
return head.isEmpty();
}
@Override
public String toString() {
return isEmpty() ? "null" : "MyEntry [head=" + head + "]";
}
}
class EntryIterator<E> implements Iterator<E> {
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return false;
}
@Override
public E next() {
// TODO Auto-generated method stub
return null;
}
@Override
public void remove() {
// TODO Auto-generated method stub
}
}