日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

如何动态的向数组中插入键值对_在Java中实现的一个简单“HashMap”

發(fā)布時間:2023/12/19 java 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何动态的向数组中插入键值对_在Java中实现的一个简单“HashMap” 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

如何創(chuàng)建Hash表

對于把K(鍵)-V(值)這樣的鍵值對插入Hash表中,需要執(zhí)行兩個步驟:

1.使用散列函數將K轉換為小整數(稱為其哈希碼)。

2.哈希碼用于查找索引(hashCode%arrSize),并且首先搜索該索引處的整個鏈表(單獨鏈)以查找已存在的K。

3.如果找到,則更新其值,如果不是,則將K-V對存儲為列表中的新節(jié)點。

復雜性和負載因子

  • 第一步,所用時間取決于K和散列函數。

例如,如果鍵是字符串“abcd”,那么它的散列函數可能取決于字符串的長度。 但是對于非常大的n值,與n相比,映射中的條目數,密鑰的長度幾乎可以忽略不計,因此可以認為散列計算在恒定時間內發(fā)生,即O(1)。

  • 對于第二步,需要遍歷存在于該索引處的K-V對列表。 為此,最壞的情況可能是所有n個條目都在相同的索引處。 因此,時間復雜度將是O(n)。 但是,已經進行了足夠的研究以使散列函數產生的鍵在數組中均勻分布,因此這幾乎不會發(fā)生。
  • 因此,平均而言,如果有n個條目且b是數組的大小,則每個索引上將有n / b個條目。 此值n / b稱為負載因子,表示hash表上的負載情況。
  • 該負載因子(Load Factor)需要保持較低,因此一個索引處的條目數較少,因此復雜度幾乎恒定,即O(1)。

Rehashing

顧名思義,rehashing意味著再次散列。 基本上,當負載因子增加到超過其預定值(負載因子的默認值為0.75)時,復雜性就會增加。因此,為了克服這個問題,數組的大小增加(加倍)并且所有值再次進行散列并存儲在新的雙倍大小的數組中,以保持低負載因子和低復雜度。

為什么要Rehashing

進行重新散列是因為每當將鍵值對插入到映射中時,負載因子增加,這意味著時間復雜度也如上所述地增加。 這可能無法提供O(1)所需的時間復雜度。

因此,必須進行重新散列,增加Bucket Array的大小,以減少負載因子和時間復雜度。

如何Rehashing

可以按如下方式進行Rehashing:

  • 對于每次向hash表添加新條目,請檢查負載因子。
  • 如果它大于其預定義值(如果沒有給出,則默認值為0.75),然后重新散列。
  • 對于Rehashing,創(chuàng)建一個比以前大小加倍的新數組,并使其成為新的Bucket Array。
  • 然后遍歷舊Bucket Array中的每個元素,并為每個元素調用insert()函數,以便將其插入到新的更大的bucket數組中。

Java程序實例

// Java program to implement Rehashing import java.util.ArrayList; class Map { class MapNode { K key; V value; MapNode next; public MapNode(K key, V value) { this.key = key; this.value = value; next = null; } } // The bucket array where // the nodes containing K-V pairs are stored ArrayList > buckets; // No. of pairs stored - n int size; // Size of the bucketArray - b int numBuckets; // Default loadFactor final double DEFAULT_LOAD_FACTOR = 0.75; public Map() { numBuckets = 5; buckets = new ArrayList<>(numBuckets); for (int i = 0; i < numBuckets; i++) { // Initialising to null buckets.add(null); } System.out.println("HashMap created"); System.out.println("Number of pairs in the Map: " + size); System.out.println("Size of Map: " + numBuckets); System.out.println("Default Load Factor : " + DEFAULT_LOAD_FACTOR + ""); } private int getBucketInd(K key) { // Using the inbuilt function from the object class int hashCode = key.hashCode(); // array index = hashCode%numBuckets return (hashCode % numBuckets); } public void insert(K key, V value) { // Getting the index at which it needs to be inserted int bucketInd = getBucketInd(key); // The first node at that index MapNode head = buckets.get(bucketInd); // First, loop through all the nodes present at that index // to check if the key already exists while (head != null) { // If already present the value is updated if (head.key.equals(key)) { head.value = value; return; } head = head.next; } // new node with the K and V MapNode newElementNode = new MapNode(key, value); // The head node at the index head = buckets.get(bucketInd); // the new node is inserted // by making it the head // and it's next is the previous head newElementNode.next = head; buckets.set(bucketInd, newElementNode); System.out.println("Pair(" + key +

總結

以上是生活随笔為你收集整理的如何动态的向数组中插入键值对_在Java中实现的一个简单“HashMap”的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。