Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Implement hashmap

class MyHashMap:

    def __init__(self):
        self.size = 10000
        self.buckets = [[] for _ in range(self.size)]
        
    def put(self, key: int, value: int) -> None:
        bucket, i = self._index(key)
        if i == -1:
            bucket.append([key,value])
            return
        bucket[i][1] = value

    def get(self, key: int) -> int:
        bucket, i = self._index(key)
        if i == -1:
            return -1
        return bucket[i][1]

    def remove(self, key: int) -> None:
        bucket, i = self._index(key)
        if i == -1: return
        bucket.pop(i)
        
    def _hash(self, key):
        return key % self.size
    
    def _index(self, key):
        hash_key = self._hash(key)
        bucket = self.buckets[hash_key]
        for i, [k,val] in enumerate(bucket):
            if k == key:
                return bucket, i
        return bucket, -1
Comment

PREVIOUS NEXT
Code Example
Java :: ldap java connection 
Java :: Designing a HashMap Key 
Java :: Spring security avec spring version 2.5.6 
Java :: why fields should be final in immutable class? 
Java :: Simple java questionnaire using json 
Java :: how to assign one value to 2 variable in one line in java 
Java :: infinity constant in java 
Java :: run app by package android 
Java :: ring write a character to the stream 
Java :: Java Insert Elements to EnumSet 
Java :: Meditation for stress and depression 
Java :: java importieren 
Java :: java jbutton hover 
Java :: how to convert string to int android studio kotlin 
Java :: can you automate mouseclicks with java 
Java :: initialize set of strings java 
Java :: Make device not run on emulator or rooted device 
Java :: rstudio boxplot coloring 
Java :: difference between set and list in java 
Java :: jaspersoft masking 
Java :: Removing DOM nodes when traversing a NodeList 
Java :: java tostring methode überschreiben 
Java :: material design implement full screen dialog android java 
Java :: java virtual override 
Java :: what is serialization in rest assured 
Java :: disarium number in java 
Java :: how to make 2 nested loops to count to 100 
Java :: Create all possible substrings of a string java 
Java :: Eclipse find/replace pluggin 
Java :: place.getlatlng() returning null 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =