Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# AlgorithmsAndDataStructures
Create ChainingHashMap and LinearProbingHashMap
109 changes: 109 additions & 0 deletions src/algorithmsAndDataStructures/ChainingHashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package algorithmsAndDataStructures;


import java.util.LinkedList;

public class ChainingHashMap<K, V> {
private int capacity;
private final int DEFAULT_CAPACITY = 16;
private int size;
private LinkedList<Node>[] st;

public ChainingHashMap(int capacity) {
if (capacity <= 0) {
throw new IllegalArgumentException();
}
this.capacity = capacity;
st = new LinkedList[capacity];
for (int i = 0; i < capacity; i++) {
st[i] = new LinkedList<>();
}
}

public ChainingHashMap() {
this.capacity = DEFAULT_CAPACITY;
st = new LinkedList[capacity];
for (int i = 0; i < capacity; i++) {
st[i] = new LinkedList<>();
}
}

private class Node {
K key;
V value;

public Node(K key, V value) {
this.key = key;
this.value = value;
}
}

public int size() {
return size;
}

public boolean isEmpty() {
return size == 0;
}

private int hash(K key) {
return (key.hashCode() & 0x7fffffff) % capacity;
}

public boolean contains(K key) {
return get(key) != null;
}

private void checkKeyNotNull(K key) {
if (key == null) {
throw new IllegalArgumentException("key = null");
}
}

public void put(K key, V value) {
checkKeyNotNull(key);
int i = hash(key);
for (Node node : st[i]) {
if (key.equals(node.key)) {
node.value = value;
return;
}
}
st[i].addLast(new Node(key, value));
size++;
}

public V get(K key) {
checkKeyNotNull(key);
int i = hash(key);
for (Node node : st[i]) {
if (key.equals(node.key)) {
return node.value;
}
}
return null;
}

public void delete(K key) {
checkKeyNotNull(key);
int i = hash(key);
for (Node node : st[i]) {
if (key.equals(node.key)) {
st[i].remove(node);
size--;
return;
}
}
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < capacity; i++) {
for (Node node : st[i]) {
sb.append("[" + node.key + "," + node.value + "] ");
}
}
return sb.toString();
}
}
67 changes: 67 additions & 0 deletions src/algorithmsAndDataStructures/LinearProbingHashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package algorithmsAndDataStructures;


public class LinearProbingHashMap<K, V> {
private int capacity;
private final int DEFAULT_CAPACITY = 17;
private int size;
private K[] keys;
private V[] values;

public LinearProbingHashMap() {
this.capacity = DEFAULT_CAPACITY;
keys = (K[]) new Object[capacity];
values = (V[]) new Object[capacity];
}

public int size() {
return size;
}

public boolean isEmpty() {
return size == 0;
}

private int hash(K key) {
return (key.hashCode() & 0x7fffffff) % capacity;
}

private int hash2(K key) {
return 7 - (key.hashCode() & 0x7fffffff) % 7;
}

private void checkKeyNotNull(K key) {
if (key == null) {
throw new IllegalArgumentException("key = null");
}
}

public void put(K key, V value) {
checkKeyNotNull(key);
if (size >= capacity - 1) {
throw new RuntimeException("map is full");
}
int i;
int step = hash2(key);
for (i = hash(key); keys[i] != null; i = (i + step) % capacity) {
if (key.equals(keys[i])) {
values[i] = value;
return;
}
}
keys[i] = key;
values[i] = value;
size++;
}

public V get(K key) {
checkKeyNotNull(key);
int step = hash2(key);
for (int i = hash(key); keys[i] != null; i = (i + step) % capacity) {
if (key.equals(keys[i])) {
return values[i];
}
}
return null;
}
}
23 changes: 22 additions & 1 deletion src/algorithmsAndDataStructures/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
public class Main {

public static void main(String[] args) {
// write your code here
doTask2();

}

public static void doTask1() {
LinearProbingHashMap<Integer, String> map = new LinearProbingHashMap<>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
System.out.println(map.toString());
System.out.println(map.get(1));
}

public static void doTask2() {
ChainingHashMap<Integer, String> map = new ChainingHashMap<>(10);
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
System.out.println(map.toString());
System.out.println(map.get(1));
map.delete(2);
System.out.println(map.toString());
}
}