Skip to content

Instantly share code, notes, and snippets.

@Brutt
Created February 12, 2018 22:54
Show Gist options
  • Save Brutt/be5428087d66b2a6b87d05b738a0562c to your computer and use it in GitHub Desktop.
Save Brutt/be5428087d66b2a6b87d05b738a0562c to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class HashMap<K, V> implements Map<K, V> {
@SuppressWarnings("unchecked")
private List<Entry<K, V>>[] buckets = new List[5];
private int size;
public HashMap() {
for (int i = 0; i < buckets.length; i++) {
buckets[i] = new ArrayList<>();
}
}
@Override
public V put(K key, V value) {
int index = getIndex(key);
List<Entry<K, V>> bucket = buckets[index];
for (Entry<K, V> entry : bucket) {
if (entry.key.equals(key)) {
V oldValue = entry.value;
entry.value = value;
return oldValue;
}
}
bucket.add(new Entry<>(key, value));
size++;
return null;
}
@Override
//if key associated with null than put else return previous value
public V putIfAbsent(K key, V value) {
V prevValue = get(key);
if (prevValue == null){
put(key, value);
}
return prevValue;
}
@Override
//
public void putAll(Map<K, V> map) {
Iterator<List<Entry<K, V>>> iterator = map.iterator();
while(iterator.hasNext()){
List<Entry<K, V>> listEntries = iterator.next();
for (Entry<K, V> entry : listEntries) {
put(entry.key, entry.value);
}
}
}
@Override
public void putAllIfAbsent(Map<K, V> map) {
Iterator<List<Entry<K, V>>> iterator = map.iterator();
while(iterator.hasNext()){
List<Entry<K, V>> listEntries = iterator.next();
for (Entry<K, V> entry : listEntries) {
putIfAbsent(entry.key, entry.value);
}
}
}
@Override
public V get(K key) {
int index = getIndex(key);
List<Entry<K, V>> bucket = buckets[index];
for (Entry<K, V> entry : bucket) {
if (entry.key.equals(key)) {
return entry.value;
}
}
return null;
}
@Override
public int size() {
return size;
}
@Override
public V remove(K key) {
return null;
}
@Override
public boolean containsKey(K key) {
return false;
}
@Override
public void clear() {
}
private int getIndex(K key) {
return key.hashCode() % buckets.length;
}
@Override
public Iterator<List<Entry<K, V>>> iterator() {
return new Itr();
}
private class Itr implements Iterator<List<Entry<K, V>>> {
int current;
Itr() {}
public boolean hasNext() {
return current != buckets.length;
}
public List<Entry<K,V>> next() {
int i = current;
current++;
return buckets[i];
}
}
private static class Entry<K, V> {
private K key;
private V value;
public Entry(K key, V value) {
this.key = key;
this.value = value;
}
}
}
public class HashMapTest {
public static void main(String[] args) {
HashMap<String, Integer> hashMap = new HashMap<>();
hashMap.put("one", 1);
hashMap.put("two", 2);
hashMap.put("three", null);
HashMap<String, Integer> map = new HashMap<>();
map.put("four", 4);
map.put("five", 5);
map.put("six", 6);
map.put("seven", 7);
map.put("three", 3);
hashMap.putAll(map);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment