久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Java HashMap 如何對“get"執行恒定時間查找 O(

How is it possible for Java HashMap to perform constant time lookup O(1) for quot;getquot; operations?(Java HashMap 如何對“get執行恒定時間查找 O(1)?操作?)
本文介紹了Java HashMap 如何對“get"執行恒定時間查找 O(1)?操作?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我了解 HashMap 如何工作的基礎知識 - hm.put(obj) 根據 obj.hashCode 值找到正確的存儲桶來放置對象.然后在該桶中,如果另一個對象 .equals(obj) 則替換它,如果沒有將其添加到桶中.

I understand the basics of how a HashMap works - hm.put(obj) finds the correct bucket to place the object into, based on the obj.hashCode value. Then within that bucket if another object .equals(obj) then replace it, if not add it to the bucket.

但我不清楚 HashMap.put 和 HashMap.get 如何可以是常數時間 O(1).據我了解,存儲桶的數量應基于哈希碼,因此將 100 個對象放入哈希圖中將(大致)創建 100 個存儲桶(我確實了解哈希碼有時會發生沖突,因此它可能小于 100 但不是經常).

But I am unclear on how HashMap.put and HashMap.get can be constant time O(1). As I understand it, the number of buckets should be based on the hashcodes, so putting 100 objects into a hashmap will (roughly) create 100 buckets (I do understand there are sometimes collisions in hashcodes, so it could be less than 100 but not often).

因此,隨著添加到哈希圖中的對象數量的增加,桶的數量也會增加 - 由于沖突很少,這并不意味著桶的數量幾乎隨著添加的對象數量線性增長,其中case HashMap.put/HashMap.get 將是 O(n),因為它必須在找到正確的存儲桶之前搜索每個存儲桶.

So as the number of objects added to a hashmap grows, so does the number of buckets - and since collisions are rare then doesn't that mean that the number of buckets grows almost linearly with the number of objects added, in which case HashMap.put/HashMap.get would be O(n) as it has to search over every bucket before it finds the right one.

我錯過了什么?

推薦答案

這是我的兩分錢,朋友.以您認為數組的方式來考慮 HashMap.事實上,它是一個數組.如果我給你索引 11,你不必遍歷數組來查找索引 11 處的對象.你只需直接去那里.這就是 HashMap 的工作原理:訣竅是使索引與值相同——本質上.

Here is my two cent, friend. Think of a HashMap the way you think of an array. In fact, it is an array. If I give you index 11, you don't have to iterate through the array to find the object at index 11. You simply go there directly. That's how a HashMap works: the trick is to make the index the same as the value -- essentially.

這就是哈希碼的用武之地.讓我們看一下哈希函數是單位乘數(即 1)的簡單情況.然后,如果你有 0 到 99 的值并且你想將它們存儲在一個數組中,那么它們將分別存儲在索引 0 到 99 處.因此 put 和 get 顯然是 O(1),因為獲取和放入數組是 O(1).現在讓我們想象一個不那么簡單的散列函數,比如 y = x+2.所以在這種情況下,值 0 到 99 將存儲在索引 2 到 101 處.這里給定一個值,比如 11,您必須計算散列以找到它或放入它(散列為 11+2 =13).好吧,哈希函數正在做一些工作來計算給定值的正確索引(在我們的例子中 y = x+2= 11+2=13).但是,這項工作所付出的努力與您擁有多少數據點無關.如果我需要放置 999 或 123,則單個 put 或 get 的工作量仍然相同: y= x+2:我每次執行 put 或 get 時只需添加兩個:這是恒定的工作.

So that is where a hashcode comes in. Let's look at the trivial case where your hash function is a unit multiplier (i.e. 1). Then if you have the values 0 to 99 and you want to store them in an array, then they will be stored at indices 0 to 99 respectively. So that a put and a get is clearly O(1) since getting and putting things in an array is O(1). Now let's imagine a less trivial hash function, say, y = x+2. So in this case the values 0 to 99 will be stored at indices 2 to 101. Here given a value, say 11, you must compute the hash to find it or put it (the hash being 11+2 =13). So okay, the hash function is doing some work to calculate the correct index given the value (in our case y = x+2= 11+2=13). But the amount of effort that goes into that work has nothing to do with how many data points you have. If I need to put 999 or 123, the amount of work for a single put or get is still the same: y= x+2: I only have to add two each time I do a put or a get: that's constant work.

您的困惑可能是您想一次放置 n 個值.那么在這種情況下,每個 put 仍然是常量 c.但是 c 乘以 n 是 c*n=O(n).因此, n 與 put 本身無關,而是您同時進行 n 個 put.我希望這個解釋會有所幫助.

Your confusion may be that you want to put n values at once. Well in that case each put is still constant c. But c multiplied by n is c*n=O(n). So the n has nothing to do with the put itself, but rather that you are making n puts all at once. I hope this explanation helps.

這篇關于Java HashMap 如何對“get"執行恒定時間查找 O(1)?操作?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Convert List of Strings into Map using Java-8 Streams API(使用 Java-8 Streams API 將字符串列表轉換為 Map)
Getting data from JSON(從 JSON 獲取數據)
java linkedhashmap iteration(javalinkedhashmap迭代)
Converting a list of objects to Map(將對象列表轉換為 Map)
Create a HashMap with a fixed Key corresponding to a HashSet. point of departure(用一個固定的Key對應一個HashSet創建一個HashMap.出發點)
HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found(HttpMessageConverter 異常:RestClientException:無法寫入請求:找不到合適的 HttpMessageConverter) - IT屋-程序員
主站蜘蛛池模板: 日韩成人免费视频 | 亚洲国产精品久久久久秋霞不卡 | 亚洲精品1区 | 国产91黄色| 久久精品一区 | 亚洲高清成人 | 午夜精品视频在线观看 | 日韩在线播放视频 | 久久久高清 | 成人美女免费网站视频 | 国产片网站 | 在线午夜电影 | 亚洲综合在 | 国产不卡一区 | 国产精品1区 | 亚洲精品久久久久中文字幕欢迎你 | 欧美一区久久 | 超碰人人在线 | 91超碰caoporn97人人 | 天天草天天 | 久久久久久久久一区 | 日本淫视频 | av天天干| 亚洲精品www久久久久久广东 | 久久午夜国产精品www忘忧草 | 奇米影视77 | 97超碰人人| 亚洲高清av在线 | 精品日韩一区二区 | 先锋av资源网 | 欧洲免费毛片 | 中国美女撒尿txxxxx视频 | 日韩av啪啪网站大全免费观看 | 久久国产精品72免费观看 | 久久男人 | 亚洲一区日韩 | 日本高清视频在线播放 | 欧美老妇交乱视频 | 日本精品视频 | 男人天堂手机在线视频 | 国产人成精品一区二区三 |