1.map接口类
package com.self.project.hashmap;
/*
* 自定義map集合
*/
public interface MyMap<K, V> {
V put(K key,V value);
V get(K key);
int size();
interface Entry<K,V>{
K getKey();
V getValue();
}
}
2.hashmap的实现底层
package com.self.project.hashmap;
/*
* 自定義hashmap集合,
*/
public class MyHashMap<K, V> implements MyMap<K, V> {
//定義鏈表數組的初始長度
private static Integer defualtLength=16;
//定義負載因子的初始值0.75
private static double defualtLoad=0.75;
//自定義數組對象
private Entry<K, V>[] table=null;
//數組的初始長度
private int size=0;
//無參構造函數
public MyHashMap() {
this(defualtLength,defualtLoad);
}
//定義一個初始化方法
@SuppressWarnings({ "static-access", "unchecked" })
private MyHashMap(Integer defualtLength, double defualtLoad) {
this.defualtLength=defualtLength;
this.defualtLoad=defualtLoad;
table=new Entry[defualtLength];
}
/**
* 放置:根據key的哈希值獲取數組對應的下標,然後根據下標判斷該位置是否有數據(因爲哈希獲取值得效率高,沒有用到遍歷)
*/
public V put(K key, V value) {
//根據key和哈希值獲取到數組中的下標
int index=this.getIndex(key);
//根據該下標判斷該位置是否有數據
Entry<K, V> e=table[index];
if (e==null) {
//如果該節點沒有值,則直接創建一個鏈表數組,便計算數組的長度
table[index]=new Entry(key, value, null, index);
size++;
}else {
//如果有值,則將原來的值進行保存到鏈表數組中,把新的值指向原來的引用位置
Entry newEntry=new Entry(key, value, e, index);
table[index]=newEntry;
}
return table[index].getValue();
}
/**
* 計算數組的下標,通過除留取余數法(取模)獲取數組的下標,(m值為數組長度的對打質數)
*
*/
private int getIndex(K key) {
int m=this.defualtLength-1;
return key.hashCode()%m;
}
public V get(K key) {
//根據哈希值獲取到數組的下標,根據下標獲取值
int index=this.getIndex(key);
return table[index]==null ? null : table[index].getValue();
}
public int size() {
return size;
}
//創建一個鏈表Entry對象(用於存放重複的值)
class Entry<K,V> implements MyMap.Entry<K, V>{
K key;
V value;
Entry<K,V> next;
int index;
public Entry(K key, V value, Entry<K, V> e, int index) {
super();
this.key = key;
this.value = value;
this.next = e;
this.index = index;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
}
}
3.测试和map的遍历方式:
package com.self.project.hashmap;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class Test {
public static void main(String[] args) {
// MyMap<Integer , String> myMap=new MyHashMap<Integer, String>();
Map<Integer , String> myMap=new HashMap<Integer, String>();
myMap.put(1, "java");
myMap.put(2, "python");
/*
* map的4中遍歷方式:value,keyset,entrySet, entrySet.iterator()
*
*/
//1.通過遍歷map的value的值,這種方式只能遍歷value的值,獲取不到key的值
for (String value : myMap.values()) {
System.out.println("遍歷的第一種方式:--》:"+value);
}
System.out.println("\n");
//2.最常用的方式,獲取map所有的key,通過遍歷key獲取key對應的value值
for (Integer key : myMap.keySet()) {
System.out.println("遍歷的第二種方式:--》:"+key+"-->value"+myMap.get(key));
}
System.out.println("\n");
//3.通過將map轉換為set集合的方式,使用增强for循環直接遍歷數據
for (Entry<Integer, String> entry : myMap.entrySet()) {
System.out.println("遍歷的第二種方式:--》:"+entry+"--->value:"+entry.getValue());
}
System.out.println("\n");
//4.转换为map集合,使用set的迭代,可以在不知道key的情况下遍历Map对象
Iterator<Entry<Integer, String>> iterator = myMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, String> entry = (Map.Entry<Integer, String>) iterator.next();
System.out.println("遍歷的第二種方式:--》:"+entry+"--->value:"+entry.getValue());
}
System.out.println(myMap.get(2));
}
}